

``` r
#INTRO: LOADING PACKAGES ETC----
library(foreign)
library(reshape2)
library(data.table)
library(stargazer)
library(MatchIt)
#library(nonrandom)
library(ggplot2)
#library(gdata1)
library(gmodels)
library(gridExtra)
library(haven)
library(plyr)
#library(car)
#library(xlsx)
library(tidyr)
library(reshape)
library(countrycode)
#library(dGlyr)
library(readr)
library(gnm)
library(nls2)
library(nlstools)
library(stats)
library(dplyr)
library(matrixStats)
library(minpack.lm)
library(lubridate)
library(AER)
library(reshape)
library(reshape2)
library(tictoc)
library(xtable)
library(tidyverse)
library(openxlsx)
library(logr)
rm(list=ls())
cat("\014")
```



``` r
#READ DATA----

d1=read_csv("cses_imd.csv")#read CSES Integrated Module Dataset (IMD)
```

```
## Rows: 281083 Columns: 276
## ── Column specification ──────────────────────────────────────────────────────────────────────────────────────
## Delimiter: ","
## chr   (9): IMD1001, IMD1002_VER, IMD1002_DOI, IMD1004, IMD1005, IMD1006, IMD1006_UN, IMD1006_NAM, IMD1008_RES
## dbl (267): IMD1003, IMD1007, IMD1008_YEAR, IMD1008_MOD_1, IMD1008_MOD_2, IMD1008_MOD_3, IMD1008_MOD_4, IMD...
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
```

``` r
d11=read_csv("cses1.csv")#read CSES Module 1: 1996-2000
```

```
## Rows: 62409 Columns: 304
## ── Column specification ──────────────────────────────────────────────────────────────────────────────────────
## Delimiter: ","
## chr   (9): A1001, A1002_VER, A1002_DOI, A1004, A1005, A1006, A1006_UN, A1006_NAM, A1009
## dbl (295): A1003, A1007, A1008, A1010_1, A1010_2, A1010_3, A1011_1, A1011_2, A1011_3, A1012_1, A1012_2, A1...
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
```

``` r
d12=read_csv("cses2.csv")#read CSES Module 2: 2001-2005
```

```
## Rows: 64256 Columns: 434
## ── Column specification ──────────────────────────────────────────────────────────────────────────────────────
## Delimiter: ","
## chr   (9): B1001, B1002_VER, B1002_DOI, B1004, B1005, B1006, B1006_UN, B1006_NAM, B1009
## dbl (425): B1003, B1007, B1008, B1010_1, B1010_2, B1010_3, B1011_1, B1011_2, B1011_3, B1012_1, B1012_2, B1...
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
```

``` r
d13=read_csv("cses3.csv")#read CSES Module 1: 2006-2010
```

```
## Rows: 80163 Columns: 448
## ── Column specification ──────────────────────────────────────────────────────────────────────────────────────
## Delimiter: ","
## chr   (9): C1001, C1002_VER, C1002_DOI, C1004, C1005, C1006, C1006_UN, C1006_NAM, C1009
## dbl (439): C1003, C1007, C1008, C1010_1, C1010_2, C1010_3, C1011_1, C1011_2, C1011_3, C1012_1, C1012_2, C1...
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
```

``` r
d14=read_csv("cses4.csv")#read CSES Module 1: 2011-2015
```

```
## Rows: 75558 Columns: 484
## ── Column specification ──────────────────────────────────────────────────────────────────────────────────────
## Delimiter: ","
## chr   (9): D1001, D1002_VER, D1002_DOI, D1004, D1005, D1006, D1006_UN, D1006_NAM, D1009
## dbl (475): D1003, D1007, D1008, D1010_1, D1010_2, D1010_3, D1011_1, D1011_2, D1011_3, D1012_1, D1012_2, D1...
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
```

``` r
d5=read_csv("cses5.csv")#read CSES Module 5: 2016-2021
```

```
## Rows: 35165 Columns: 526
## ── Column specification ──────────────────────────────────────────────────────────────────────────────────────
## Delimiter: ","
## chr   (9): E1001, E1002_VER, E1002_DOI, E1004, E1005, E1006, E1006_UN, E1006_NAM, E1009
## dbl (517): E1003, E1007, E1008, E1010_1, E1010_2, E1010_3, E1011_1, E1011_2, E1011_3, E1012_1, E1012_2, E1...
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
```

``` r
#CSES Module 1: 1996-2001
#CSES Module 2: 2001-2006
#CSES Module 3: 2006-2011
#CSES Module 4: 2011-2016
#CSES Module 5: 2016-2021


#ELECTION EXCLUSIONS----
#elections exclusion because no data on leader likability:
d1=d1[!(d1$IMD1008_MOD_2==1&d1$IMD1004!="PRT_2002"&d1$IMD1004!="AUS_2004"&d1$IMD1004!="BRA_2002"),]#in CSES#2
d1=d1[d1$IMD1004!="BELF1999",]#BELF1999: IMD3009_ no data on leader likability
d1=d1[d1$IMD1004!="BELW1999",]#BELW1999: IMD3009_ no data on leader likability

#elections exclusion for other reasons (alphabetical):
d1=d1[d1$IMD1004!="AUT_2008",]#no leader likability question
d1=d1[d1$IMD1004!="BLR_2001",]#only presidential election
d1=d1[d1$IMD1004!="BLR_2008",]#>50% NAs on vote choice
d1=d1[d1$IMD1004!="BRA_2002",]#>50% NAs on vote choice
d1=d1[d1$IMD1004!="BRA_2006",]#>50% NAs on vote choice
d1=d1[d1$IMD1004!="BRA_2010",]#>50% NAs on vote choice
d1=d1[d1$IMD1004!="BRA_2014",]#>50% NAs on vote choice
d1=d1[d1$IMD1004!="CHL_1999",]#only presidential election
d1=d1[d1$IMD1004!="CHL_2009",]#binominal system
d5=d5[d5$E1004!="CHL_2017",]#>50% NAs on vote choice
d1=d1[d1$IMD1004!="EST_2011",]#double/multiple voting ###
d1=d1[d1$IMD1004!="FRA_2012",]#only presidential election
d1=d1[d1$IMD1004!="HKG_1998",]#non free Freedom House
d1=d1[d1$IMD1004!="HKG_2000",]#non free Freedom House
d1=d1[d1$IMD1004!="HKG_2008",]#non free Freedom House
d1=d1[d1$IMD1004!="HKG_2012",]#non free Freedom House
d5=d5[d5$E1004!="HKG_2016",]#non free Freedom House
d1=d1[d1$IMD1004!="HUN_1998",]#parallel voting ###
d5=d5[d5$E1004!="HUN_2018",]#parallel voting ###
d1=d1[d1$IMD1004!="KEN_2013",]#two party elections
d1=d1[d1$IMD1004!="JPN_1996",]#parallel voting ###
d1=d1[d1$IMD1004!="JPN_2007",]#only upper house election ###
d1=d1[d1$IMD1004!="JPN_2013",]#only upper house election ###
d1=d1[d1$IMD1004!="KOR_2000",]#parallel voting ###
d1=d1[d1$IMD1004!="KOR_2008",]#parallel voting ###
d1=d1[d1$IMD1004!="KOR_2012",]#parallel voting ###
d5=d5[d5$E1004!="KOR_2016",]#parallel voting ###
d1=d1[d1$IMD1004!="LTU_1997",]#only presidential election
d5=d5[d5$E1004!="LTU_2016",]#parallel voting ###
d1=d1[d1$IMD1004!="MEX_1997",]#non free Freedom House
d1=d1[d1$IMD1004!="MEX_2012",]#non free Freedom House
d1=d1[d1$IMD1004!="MEX_2015",]#non free Freedom House
d5=d5[d5$E1004!="MNE_2016",]#non free Freedom House
d1=d1[d1$IMD1004!="PER_2000",]#non free Freedom House
d1=d1[d1$IMD1004!="PHL_2010",]#only presidential election
d1=d1[d1$IMD1004!="PHL_2016",]#non free Freedom House
d1=d1[d1$IMD1004!="ROU_1996",]#coalition coding not reliable
d1=d1[d1$IMD1004!="ROU_2009",]#only presidential election
d1=d1[d1$IMD1004!="ROU_2012",]#coalition coding not reliable
d1=d1[d1$IMD1004!="ROU_2014",]#only presidential election
d1=d1[d1$IMD1004!="RUS_1999",]#non free Freedom House
d1=d1[d1$IMD1004!="RUS_2000",]#non free Freedom House
d1=d1[d1$IMD1004!="CHE_1999",]#double/multiple voting ###
d1=d1[d1$IMD1004!="CHE_2007",]#double/multiple voting ###
d1=d1[d1$IMD1004!="CHE_2011",]#double/multiple voting ###
d1=d1[d1$IMD1004!="TWN_2008",]#only presidential election
d1=d1[d1$IMD1004!="TWN_2012",]#parallel voting ###
d5=d5[d5$E1004!="TWN_2016",]#parallel voting ###
d1=d1[d1$IMD1004!="THA_2001",]#missing vote choice data
d1=d1[d1$IMD1004!="THA_2007",]##non free Freedom House
d1=d1[d1$IMD1004!="THA_2011",]##non free Freedom House
d1=d1[d1$IMD1004!="TUR_2011",]#non free Freedom House
d1=d1[d1$IMD1004!="TUR_2015",]#non free Freedom House
d1=d1[d1$IMD1004!="UKR_1998",]#non free Freedom House
d1=d1[d1$IMD1004!="USA_1996",]#two party elections
d1=d1[d1$IMD1004!="USA_2008",]#two party elections
d1=d1[d1$IMD1004!="USA_2012",]#two party elections
d5=d5[d5$E1004!="USA_2016",]#two party elections
d1=d1[d1$IMD1004!="URY_2009",]#compulsory primaries to choose leader

#exclude for same reasons elections in second release CSES wave 5:
d5=d5[!(d5$E1004 %in% c("BRA_2018","CHL_2017","FRA_2017","HKG_2016","HUN_2018","KOR_2016","LTU_2016","MNE_2016","TUR_2018","TWN_2016","USA_2016")),]

d5$E1004[d5$E1004=="GRC_2015"]="GRC_2016"#temporarily modifying 2015s year to code all the variables with
#unique election-year, it will be modified before the analysis


#CODING ELECTIONS DETAILS:----
#compensatory voting: DEU; HUN
#double voting for local candidate but for party for share of seats: NZL
#single voting for local candidate also valid for party for share of seats: TWN
#single transferrable vote: IRL

#general:
d1$NP=NA
d1$CoalCode=0
d5$NP=NA
d5$CoalCode=0
#Argentina:#
d1$CoalCode[d1$IMD1004=="ARG_2015"]=1
d1$NP[d1$IMD1004=="ARG_2015"]=6
#Australia:#
d1$CoalCode[d1$IMD1004=="AUS_1996"]=1
d1$CoalCode[d1$IMD1004=="AUS_2004"]=1
d1$CoalCode[d1$IMD1004=="AUS_2007"]=1
d1$CoalCode[d1$IMD1004=="AUS_2013"]=1
d5$CoalCode[d5$E1004=="AUS_2019"]=1#
d1$NP[d1$IMD1004=="AUS_1996"]=4
d1$NP[d1$IMD1004=="AUS_2004"]=6#scaled up since E not coded
d1$NP[d1$IMD1004=="AUS_2007"]=4
d1$NP[d1$IMD1004=="AUS_2013"]=4#scaled up E since D not coded
d5$NP[d5$E1004=="AUS_2019"]=6#
d1$IMD3008_E[d1$IMD1004=="AUS_1996"]=NA#party likability: E,F not coded
d1$IMD3008_F[d1$IMD1004=="AUS_1996"]=NA
d1$IMD3009_E[d1$IMD1004=="AUS_1996"]=NA#leader likability: E,F not coded
d1$IMD3009_F[d1$IMD1004=="AUS_1996"]=NA
d1$IMD3008_E[d1$IMD1004=="AUS_2004"]=d1$IMD3008_F[d1$IMD1004=="AUS_2004"]#party likability: scaled up since E not coded
d1$IMD3008_F[d1$IMD1004=="AUS_2004"]=d1$IMD3008_G[d1$IMD1004=="AUS_2004"]
d1$IMD3009_E[d1$IMD1004=="AUS_2004"]=d1$IMD3009_F[d1$IMD1004=="AUS_2004"]#leader likability: scaled up since E not coded
d1$IMD3009_F[d1$IMD1004=="AUS_2004"]=d1$IMD3009_G[d1$IMD1004=="AUS_2004"]
d1$IMD3008_E[d1$IMD1004=="AUS_2007"]=NA#party likability: E,F not coded
d1$IMD3008_F[d1$IMD1004=="AUS_2007"]=NA
d1$IMD3009_E[d1$IMD1004=="AUS_2007"]=NA#leader likability: E,F not coded
d1$IMD3009_F[d1$IMD1004=="AUS_2007"]=NA
d1$IMD3008_D[d1$IMD1004=="AUS_2013"]=d1$IMD3008_E[d1$IMD1004=="AUS_2013"]#party likability: scaled up E since D not coded
d1$IMD3009_D[d1$IMD1004=="AUS_2013"]=d1$IMD3009_E[d1$IMD1004=="AUS_2013"]#leader likability: scaled up E since D not coded
d1$IMD3008_E[d1$IMD1004=="AUS_2013"]=NA#party likability: E,F not coded
d1$IMD3008_F[d1$IMD1004=="AUS_2013"]=NA
d1$IMD3009_E[d1$IMD1004=="AUS_2013"]=NA#leader likability: E,F not coded
d1$IMD3009_F[d1$IMD1004=="AUS_2013"]=NA
d1$IMD3007_E[d1$IMD1004=="AUS_1996"]=NA#LRP: E,F not coded
d1$IMD3007_F[d1$IMD1004=="AUS_1996"]=NA
d1$IMD3007_E[d1$IMD1004=="AUS_2004"]=d1$IMD3007_F[d1$IMD1004=="AUS_2004"]#LRP: scaled up since E not coded
d1$IMD3007_F[d1$IMD1004=="AUS_2004"]=d1$IMD3007_G[d1$IMD1004=="AUS_2004"]
d1$IMD3007_E[d1$IMD1004=="AUS_2007"]=NA#LRP: E,F not coded
d1$IMD3007_F[d1$IMD1004=="AUS_2007"]=NA
d1$IMD3007_D[d1$IMD1004=="AUS_2013"]=d1$IMD3007_E[d1$IMD1004=="AUS_2013"]#LRP: scaled up E since D not coded
d1$IMD3007_E[d1$IMD1004=="AUS_2013"]=NA#LRP: E,F not coded
d1$IMD3007_F[d1$IMD1004=="AUS_2013"]=NA
#Austria:#
d1$NP[d1$IMD1004=="AUT_2013"]=6
d5$NP[d5$E1004=="AUT_2017"]=6
#Bulgaria:#
d1$NP[d1$IMD1004=="BGR_2014"]=6
#Canada:#
d1$NP[d1$IMD1004=="CAN_1997"]=5
d1$NP[d1$IMD1004=="CAN_2008"]=5
d1$NP[d1$IMD1004=="CAN_2011"]=5
d1$NP[d1$IMD1004=="CAN_2015"]=5
d11$A3020_E[d11$A1004=="CAN_1997"&d11$A2019!=24]=NA#set party like Bloc Quebecois as NA outside Quebec
#NB: this choice is what was done in the Canadian NES, check for example highlights in NES 1997, 1993;
# which are supposedly collected by the EV dataset. Also, since 1997 appears in both CSES amd EV
# I checked the ratio of NAs for the Bloc Quebecois after setting it NA in Quebec for the CSES,
# and the proportion is the same. Therefore I set NA for Bloc Quebecois outside Quebec,
# and analogously I set NA party Reform inside Quebec.
d1$IMD3008_E[d1$IMD1004=="CAN_1997"]=d11$A3020_E[d11$A1004=="CAN_1997"]
d13$C3009_C[d13$C1004=="CAN_2008"&d13$C2027!=24]=NA
d1$IMD3008_C[d1$IMD1004=="CAN_2008"]=d13$C3009_C[d13$C1004=="CAN_2008"]
d14$D3011_D[d14$D1004=="CAN_2011"&d14$D2028!=24]=NA
d1$IMD3008_D[d1$IMD1004=="CAN_2011"]=d14$D3011_D[d14$D1004=="CAN_2011"]
d14$D3011_D[d14$D1004=="CAN_2015"&d14$D2028!=24]=NA
d1$IMD3008_D[d1$IMD1004=="CAN_2015"]=d14$D3011_D[d14$D1004=="CAN_2015"]
d11$A3020_B[d11$A1004=="CAN_1997"&d11$A2019==24]=NA#set party like Reform as NA inside Quebec
d1$IMD3008_B[d1$IMD1004=="CAN_1997"]=d11$A3020_B[d11$A1004=="CAN_1997"]
d11$A3021_E[d11$A1004=="CAN_1997"&d11$A2019!=24]=NA#set leader like Bloc Quebecois as NA outside Quebec
d1$IMD3009_E[d1$IMD1004=="CAN_1997"]=d11$A3021_E[d11$A1004=="CAN_1997"]
d13$C3010_C[d13$C1004=="CAN_2008"&d13$C2027!=24]=NA
d1$IMD3009_C[d1$IMD1004=="CAN_2008"]=d13$C3010_C[d13$C1004=="CAN_2008"]
d14$D3012_D[d14$D1004=="CAN_2011"&d14$D2028!=24]=NA
d1$IMD3009_D[d1$IMD1004=="CAN_2011"]=d14$D3012_D[d14$D1004=="CAN_2011"]
d14$D3012_D[d14$D1004=="CAN_2015"&d14$D2028!=24]=NA
d1$IMD3009_D[d1$IMD1004=="CAN_2015"]=d14$D3012_D[d14$D1004=="CAN_2015"]
d11$A3021_B[d11$A1004=="CAN_1997"&d11$A2019==24]=NA#set leader like Reform as NA inside Quebec
d1$IMD3009_B[d1$IMD1004=="CAN_1997"]=d11$A3021_B[d11$A1004=="CAN_1997"]
d11$A3032_E[d11$A1004=="CAN_1997"&d11$A2019!=24]=NA#set LRP Bloc Quebecois as NA outside Quebec
d1$IMD3007_E[d1$IMD1004=="CAN_1997"]=d11$A3032_E[d11$A1004=="CAN_1997"]
d13$C3011_C[d13$C1004=="CAN_2008"&d13$C2027!=24]=NA
d1$IMD3007_C[d1$IMD1004=="CAN_2008"]=d13$C3011_C[d13$C1004=="CAN_2008"]
d14$D3013_D[d14$D1004=="CAN_2011"&d14$D2028!=24]=NA
d1$IMD3007_D[d1$IMD1004=="CAN_2011"]=d14$D3013_D[d14$D1004=="CAN_2011"]
d14$D3013_D[d14$D1004=="CAN_2015"&d14$D2028!=24]=NA
d1$IMD3007_D[d1$IMD1004=="CAN_2015"]=d14$D3013_D[d14$D1004=="CAN_2015"]
d11$A3032_B[d11$A1004=="CAN_1997"&d11$A2019==24]=NA#set LRP Reform as NA inside Quebec
d1$IMD3007_B[d1$IMD1004=="CAN_1997"]=d11$A3032_B[d11$A1004=="CAN_1997"]
#Croatia:#
d1$CoalCode[d1$IMD1004=="HRV_2007"]=1
d1$NP[d1$IMD1004=="HRV_2007"]=6
d1$IMD5001_A[d1$IMD1004=="HRV_2007"]=36.6#recoding HRV_2007 percent vote (error)
d1$IMD5001_B[d1$IMD1004=="HRV_2007"]=31.3
d1$IMD5001_C[d1$IMD1004=="HRV_2007"]=6.7
#Czech Republic:#
d1$NP[d1$IMD1004=="CZE_1996"]=6
d1$NP[d1$IMD1004=="CZE_2006"]=6
d1$NP[d1$IMD1004=="CZE_2010"]=6
d1$NP[d1$IMD1004=="CZE_2013"]=6
#Denmark:#
d1$NP[d1$IMD1004=="DNK_1998"]=6
d1$NP[d1$IMD1004=="DNK_2007"]=6
#Finland:#
d1$NP[d1$IMD1004=="FIN_2007"]=6
d1$NP[d1$IMD1004=="FIN_2011"]=6
d1$NP[d1$IMD1004=="FIN_2015"]=6
#France:#
d1$NP[d1$IMD1004=="FRA_2007"]=6
#Germany:#
d1$CoalCode[d1$IMD1004=="DEU_1998"]=1
d1$CoalCode[d1$IMD1004=="DEU_2005"]=1
d1$CoalCode[d1$IMD1004=="DEU_2009"]=1
d1$CoalCode[d1$IMD1004=="DEU_2013"]=1
d5$CoalCode[d5$E1004=="DEU_2017"]=1
d1$NP[d1$IMD1004=="DEU_1998"]=5
d1$NP[d1$IMD1004=="DEU_2005"]=6
d1$NP[d1$IMD1004=="DEU_2009"]=5
d1$NP[d1$IMD1004=="DEU_2013"]=5
d5$NP[d5$E1004=="DEU_2017"]=6
d1$IMD5001_A[d1$IMD1004=="DEU_1998"]=40.9#recoding DEU_1998 percent vote
d1$IMD5001_B[d1$IMD1004=="DEU_1998"]=28.4#(they coded LH instead than PL vote share)
d1$IMD5001_C[d1$IMD1004=="DEU_1998"]=6.7
d1$IMD5001_D[d1$IMD1004=="DEU_1998"]=6.7
d1$IMD5001_E[d1$IMD1004=="DEU_1998"]=6.2
d1$IMD5001_F[d1$IMD1004=="DEU_1998"]=5.1
d11$A3020_B[d11$A1004=="DEU_1998"&d11$A2019==9]=d11$A3020_C[d11$A1004=="DEU_1998"&d11$A2019==9]#set party like CDU as CSU in Bavaria
#this is also how the TEV is coded
d1$IMD3008_B[d1$IMD1004=="DEU_1998"]=d11$A3020_B[d11$A1004=="DEU_1998"]

d1$IMD3008_C[d1$IMD1004=="DEU_1998"]=d1$IMD3008_D[d1$IMD1004=="DEU_1998"]#party likability scale up letters after removing CSU
d1$IMD3008_D[d1$IMD1004=="DEU_1998"]=d1$IMD3008_E[d1$IMD1004=="DEU_1998"]
d1$IMD3008_E[d1$IMD1004=="DEU_1998"]=d1$IMD3008_F[d1$IMD1004=="DEU_1998"]
d1$IMD3008_F[d1$IMD1004=="DEU_1998"]=NA
d13$C3009_B[d13$C1004=="DEU_2005"&d13$C2027==9]=d13$C3009_F[d13$C1004=="DEU_2005"&d13$C2027==9]#set party like CDU as CSU in Bavaria
d1$IMD3008_B[d1$IMD1004=="DEU_2005"]=d13$C3009_B[d13$C1004=="DEU_2005"]
d1$IMD3008_F[d1$IMD1004=="DEU_2005"]=d1$IMD3008_G[d1$IMD1004=="DEU_2005"]#party likability scale up letters after removing CSU
d13$C3009_A[d13$C1004=="DEU_2009"&d13$C2027==9]=d13$C3009_F[d13$C1004=="DEU_2009"&d13$C2027==9]#set party like CDU as CSU in Bavaria
d1$IMD3008_A[d1$IMD1004=="DEU_2009"]=d13$C3009_A[d13$C1004=="DEU_2009"]
d1$IMD3008_F[d1$IMD1004=="DEU_2009"]=NA#party likability scale up letters after removing CSU
d14$D3011_A[d14$D1004=="DEU_2013"&d14$D2028==2]=d14$D3011_E[d14$D1004=="DEU_2013"&d14$D2028==2]#set party like CDU as CSU in Bavaria
d1$IMD3008_A[d1$IMD1004=="DEU_2013"]=d14$D3011_A[d14$D1004=="DEU_2013"]
d1$IMD3008_E[d1$IMD1004=="DEU_2013"]=d1$IMD3008_F[d1$IMD1004=="DEU_2013"]#party likability scale up letters after removing CSU
d1$IMD3008_F[d1$IMD1004=="DEU_2013"]=NA
d5$E3017_A[d5$E1004=="DEU_2017"&d5$E2020==2]=d5$E3017_G[d5$E1004=="DEU_2017"&d5$E2020==2]#set party like CDU as CSU in Bavaria
# d5$E3017_A[d5$E1004=="DEU_2017"&d5$E2020==2]=rowMeans(d5[d5$E1004=="DEU_2017"&d5$E2020==2,names(d5) %in% c("E3017_A","E3017_G")],na.rm=T)#set party like CDU as mean CDU and CSU in Bavaria
d11$A3021_B[d11$A1004=="DEU_1998"&d11$A2019==9]=d11$A3021_C[d11$A1004=="DEU_1998"&d11$A2019==9]#set leader like CDU as CSU in Bavaria
d1$IMD3009_B[d1$IMD1004=="DEU_1998"]=d11$A3021_B[d11$A1004=="DEU_1998"]
d1$IMD3009_C[d1$IMD1004=="DEU_1998"]=d1$IMD3009_D[d1$IMD1004=="DEU_1998"]#leader likability scale up letters after removing CSU
d1$IMD3009_D[d1$IMD1004=="DEU_1998"]=d1$IMD3009_E[d1$IMD1004=="DEU_1998"]
d1$IMD3009_E[d1$IMD1004=="DEU_1998"]=d1$IMD3009_F[d1$IMD1004=="DEU_1998"]
d1$IMD3009_F[d1$IMD1004=="DEU_1998"]=NA
d13$C3010_B[d13$C1004=="DEU_2005"&d13$C2027==9]=d13$C3010_F[d13$C1004=="DEU_2005"&d13$C2027==9]#set leader like CDU as CSU in Bavaria
d1$IMD3009_B[d1$IMD1004=="DEU_2005"]=d13$C3010_B[d13$C1004=="DEU_2005"]
d1$IMD3009_F[d1$IMD1004=="DEU_2005"]=d1$IMD3009_G[d1$IMD1004=="DEU_2005"]#leader likability scale up letters after removing CSU
d13$C3010_A[d13$C1004=="DEU_2009"&d13$C2027==9]=d13$C3010_F[d13$C1004=="DEU_2009"&d13$C2027==9]#set leader like CDU as CSU in Bavaria
d1$IMD3009_A[d1$IMD1004=="DEU_2009"]=d13$C3010_A[d13$C1004=="DEU_2009"]
d1$IMD3009_F[d1$IMD1004=="DEU_2009"]=NA#party likability scale up letters after removing CSU
d14$D3012_A[d14$D1004=="DEU_2013"&d14$D2028==2]=d14$D3012_E[d14$D1004=="DEU_2013"&d14$D2028==2]
d1$IMD3009_A[d1$IMD1004=="DEU_2013"]=d14$D3012_A[d14$D1004=="DEU_2013"]
d1$IMD3009_E[d1$IMD1004=="DEU_2013"]=d1$IMD3009_F[d1$IMD1004=="DEU_2013"]#party likability scale up letters after removing CSU
d1$IMD3009_F[d1$IMD1004=="DEU_2013"]=NA
d5$E3018_A[d5$E1004=="DEU_2017"&d5$E2020==2]=d5$E3018_G[d5$E1004=="DEU_2017"&d5$E2020==2]#set leader like CDU as CSU in Bavaria
d11$A3032_B[d11$A1004=="DEU_1998"&d11$A2019==9]=d11$A3032_C[d11$A1004=="DEU_1998"&d11$A2019==9]#set LRP CDU as CSU in Bavaria
d1$IMD3007_B[d1$IMD1004=="DEU_1998"]=d11$A3032_B[d11$A1004=="DEU_1998"]
d1$IMD3007_C[d1$IMD1004=="DEU_1998"]=d1$IMD3007_D[d1$IMD1004=="DEU_1998"]#LRP scale up letters after removing CSU
d1$IMD3007_D[d1$IMD1004=="DEU_1998"]=d1$IMD3007_E[d1$IMD1004=="DEU_1998"]
d1$IMD3007_E[d1$IMD1004=="DEU_1998"]=d1$IMD3007_F[d1$IMD1004=="DEU_1998"]
d1$IMD3007_F[d1$IMD1004=="DEU_1998"]=NA
d13$C3011_B[d13$C1004=="DEU_2005"&d13$C2027==9]=d13$C3011_F[d13$C1004=="DEU_2005"&d13$C2027==9]#set LRP CDU as CSU in Bavaria
d1$IMD3007_B[d1$IMD1004=="DEU_2005"]=d13$C3011_B[d13$C1004=="DEU_2005"]
d1$IMD3007_F[d1$IMD1004=="DEU_2005"]=d1$IMD3007_G[d1$IMD1004=="DEU_2005"]#LRP scale up letters after removing CSU
d13$C3011_A[d13$C1004=="DEU_2009"&d13$C2027==9]=d13$C3011_F[d13$C1004=="DEU_2009"&d13$C2027==9]#set LRP CDU as CSU in Bavaria
d1$IMD3007_A[d1$IMD1004=="DEU_2009"]=d13$C3011_A[d13$C1004=="DEU_2009"]
d1$IMD3007_F[d1$IMD1004=="DEU_2009"]=NA#LRP scale up letters after removing CSU
d14$D3013_A[d14$D1004=="DEU_2013"&d14$D2028==2]=d14$D3013_E[d14$D1004=="DEU_2013"&d14$D2028==2]#set LRP CDU as CSU in Bavaria
d1$IMD3007_A[d1$IMD1004=="DEU_2013"]=d14$D3013_A[d14$D1004=="DEU_2013"]
d1$IMD3007_E[d1$IMD1004=="DEU_2013"]=d1$IMD3007_F[d1$IMD1004=="DEU_2013"]#LRP scale up letters after removing CSU
d1$IMD3007_F[d1$IMD1004=="DEU_2013"]=NA
d5$E3019_A[d5$E1004=="DEU_2017"&d5$E2020==2]=d5$E3019_G[d5$E1004=="DEU_2017"&d5$E2020==2]#set LRP CDU as CSU in Bavaria

#exclude East Germany in order to have West Germany as congruent electorate (and exclude Eastern Europe):
d11_temp=d11[d11$A1004=="DEU_1998",c("A1005","A2019")]#keep only DEU_1998, RespID and region
d1=merge(d1, d11_temp, by.x=c("IMD1005"), by.y=c("A1005"), all.x=T)
d13_temp=d13[d13$C1004=="DEU_2005",c("C1005","C2027")]#keep only DEU_2005, RespID and region
d1=merge(d1, d13_temp, by.x=c("IMD1005"), by.y=c("C1005"), all.x=T)
colnames(d1)[colnames(d1)=="C2027"]="C2027_"
d13_temp=d13[d13$C1004=="DEU_2009",c("C1005","C2027")]#keep only DEU_1998, RespID and region
d1=merge(d1, d13_temp, by.x=c("IMD1005"), by.y=c("C1005"), all.x=T)
d14_temp=d14[d14$D1004=="DEU_2013",c("D1005","D2028")]#keep only DEU_1998, RespID and region
d1=merge(d1, d14_temp, by.x=c("IMD1005"), by.y=c("D1005"), all.x=T)
d1=d1[!(d1$A2019 %in% c(11,12,13,14,15,16,17)),]#filter out East Germany regions
d1=d1[!(d1$C2027_ %in% c(11,12,13,14,15,16,17)),]
d1=d1[!(d1$C2027 %in% c(11,12,13,14,15,16,17)),]
d1=d1[!(d1$D2028 %in% c(3,4,8,13,14,16)),]
d1=d1[,-which(names(d1) %in% c("A2019","C2027_","C2027","D2028"))]#remove working variables and datasets:
rm(d11_temp,d13_temp,d14_temp)
d5=d5[!(d5$E1004=="DEU_2017" & d5$E2020 %in% c(3,4,8,13,14,16)),]#remove East Germany in CSES5

#Great Britain:#
d1$NP[d1$IMD1004=="GBR_1997"]=5
d1$NP[d1$IMD1004=="GBR_2015"]=6
#Greece:# (2009 different law, but almost same disproportional mechanism, kept the same)
d1$NP[d1$IMD1004=="GRC_2009"]=6
d1$NP[d1$IMD1004=="GRC_2012"]=6
d1$NP[d1$IMD1004=="GRC_2015"]=6
d5$NP[d5$E1004=="GRC_2016"]=6
#Iceland:#
d1$NP[d1$IMD1004=="ISL_1999"]=5
d1$NP[d1$IMD1004=="ISL_2007"]=6
d1$NP[d1$IMD1004=="ISL_2009"]=6
d1$NP[d1$IMD1004=="ISL_2013"]=6
d5$NP[d5$E1004=="ISL_2016"]=6#
d5$NP[d5$E1004=="ISL_2017"]=6#
#Ireland:#
d1$NP[d1$IMD1004=="IRL_2007"]=6
d1$NP[d1$IMD1004=="IRL_2011"]=5
d5$NP[d5$E1004=="IRL_2016"]=6
d1$IMD3008_E[d1$IMD1004=="IRL_2011"]=d1$IMD3008_F[d1$IMD1004=="IRL_2011"]#party likability: E not coded, substituted with F
d1$IMD3008_F[d1$IMD1004=="IRL_2011"]=NA
d1$IMD3009_E[d1$IMD1004=="IRL_2011"]=d1$IMD3009_F[d1$IMD1004=="IRL_2011"]#leader likability: E not coded, substituted with F
d1$IMD3009_F[d1$IMD1004=="IRL_2011"]=NA
d1$IMD3007_E[d1$IMD1004=="IRL_2011"]=d1$IMD3007_F[d1$IMD1004=="IRL_2011"]#LRP: E not coded, substituted with F
d1$IMD3007_F[d1$IMD1004=="IRL_2011"]=NA
#Israel:#
d1$NP[d1$IMD1004=="ISR_1996"]=5
d1$NP[d1$IMD1004=="ISR_2006"]=6
d1$NP[d1$IMD1004=="ISR_2013"]=6
d1$IMD3008_F[d1$IMD1004=="ISR_2013"]=d1$IMD3008_G[d1$IMD1004=="ISR_2013"]#party likability: F not coded, substituted with G
d1$IMD3009_F[d1$IMD1004=="ISR_2013"]=d1$IMD3009_G[d1$IMD1004=="ISR_2013"]#leader likability: F not coded, substituted with G
d1$IMD3007_F[d1$IMD1004=="ISR_2013"]=d1$IMD3007_G[d1$IMD1004=="ISR_2013"]#party likability: F not coded, substituted with G
#Italy:#
d5$CoalCode[d5$E1004=="ITA_2018"]=1
d5$NP[d5$E1004=="ITA_2018"]=6
d5$E5002_B[d5$E1004=="ITA_2018"]=17.8#recoding ITA_2018 percent seats (error)
#Latvia:#
d1$CoalCode[d1$IMD1004=="LVA_2010"]=1
d1$CoalCode[d1$IMD1004=="LVA_2011"]=1
d1$CoalCode[d1$IMD1004=="LVA_2014"]=1
d1$NP[d1$IMD1004=="LVA_2010"]=6
d1$NP[d1$IMD1004=="LVA_2011"]=6
d1$NP[d1$IMD1004=="LVA_2014"]=6
#Mexico:#
d1$CoalCode[d1$IMD1004=="MEX_2000"]=1
d1$CoalCode[d1$IMD1004=="MEX_2006"]=1
d1$CoalCode[d1$IMD1004=="MEX_2009"]=1
d1$NP[d1$IMD1004=="MEX_2000"]=3
d1$NP[d1$IMD1004=="MEX_2006"]=3
d1$NP[d1$IMD1004=="MEX_2009"]=3
d1$IMD5002_A[d1$IMD1004=="MEX_2000"]=44.6#recoding MEX_2000 percent seats (error)
d1$IMD5002_B[d1$IMD1004=="MEX_2000"]=42.2
d1$IMD5002_C[d1$IMD1004=="MEX_2000"]=13.2
d1$IMD5002_A[d1$IMD1004=="MEX_2006"]=41.2#recoding MEX_2006 percent seats (error)
d1$IMD5002_B[d1$IMD1004=="MEX_2006"]=31.6
d1$IMD5002_C[d1$IMD1004=="MEX_2006"]=24.6
d1$IMD5001_A[d1$IMD1004=="MEX_2009"]=36.9#recoding MEX_2009 percent vote (error)
d1$IMD5001_B[d1$IMD1004=="MEX_2009"]=28.0
d1$IMD5001_C[d1$IMD1004=="MEX_2009"]=12.2
d1$IMD5002_A[d1$IMD1004=="MEX_2009"]=47.4#recoding MEX_2006 percent seats (error)
d1$IMD5002_B[d1$IMD1004=="MEX_2009"]=28.6
d1$IMD5002_C[d1$IMD1004=="MEX_2009"]=14.2
d1$IMD3008_D[d1$IMD1004=="MEX_2000"]=NA#party likability MEX: D,E,F not coded
d1$IMD3008_E[d1$IMD1004=="MEX_2000"]=NA
d1$IMD3008_F[d1$IMD1004=="MEX_2000"]=NA
d1$IMD3008_D[d1$IMD1004=="MEX_2006"]=NA
d1$IMD3008_E[d1$IMD1004=="MEX_2006"]=NA
d1$IMD3008_F[d1$IMD1004=="MEX_2006"]=NA
d1$IMD3008_D[d1$IMD1004=="MEX_2009"]=NA
d1$IMD3008_E[d1$IMD1004=="MEX_2009"]=NA
d1$IMD3008_F[d1$IMD1004=="MEX_2009"]=NA
d1$IMD3009_D[d1$IMD1004=="MEX_2000"]=NA#leader likability MEX: D,E,F not coded
d1$IMD3009_E[d1$IMD1004=="MEX_2000"]=NA
d1$IMD3009_F[d1$IMD1004=="MEX_2000"]=NA
d1$IMD3009_D[d1$IMD1004=="MEX_2006"]=NA
d1$IMD3009_E[d1$IMD1004=="MEX_2006"]=NA
d1$IMD3009_F[d1$IMD1004=="MEX_2006"]=NA
d1$IMD3009_D[d1$IMD1004=="MEX_2009"]=NA
d1$IMD3009_E[d1$IMD1004=="MEX_2009"]=NA
d1$IMD3009_F[d1$IMD1004=="MEX_2009"]=NA
d1$IMD3007_D[d1$IMD1004=="MEX_2000"]=NA#LRP MEX: D,E,F not coded
d1$IMD3007_E[d1$IMD1004=="MEX_2000"]=NA
d1$IMD3007_F[d1$IMD1004=="MEX_2000"]=NA
d1$IMD3007_D[d1$IMD1004=="MEX_2006"]=NA
d1$IMD3007_E[d1$IMD1004=="MEX_2006"]=NA
d1$IMD3007_F[d1$IMD1004=="MEX_2006"]=NA
d1$IMD3007_D[d1$IMD1004=="MEX_2009"]=NA
d1$IMD3007_E[d1$IMD1004=="MEX_2009"]=NA
d1$IMD3007_F[d1$IMD1004=="MEX_2009"]=NA
#Montenegro:#
d1$CoalCode[d1$IMD1004=="MNE_2012"]=1
d1$NP[d1$IMD1004=="MNE_2012"]=6
#Netherlands:#
d1$NP[d1$IMD1004=="NLD_1998"]=6
d1$NP[d1$IMD1004=="NLD_2006"]=6
d1$NP[d1$IMD1004=="NLD_2010"]=6
#New Zealand:#
d1$NP[d1$IMD1004=="NZL_1996"]=6
d1$NP[d1$IMD1004=="NZL_2008"]=6
d1$NP[d1$IMD1004=="NZL_2011"]=6
d1$NP[d1$IMD1004=="NZL_2014"]=6
d5$NP[d5$E1004=="NZL_2017"]=6#
d1$IMD5001_A[d1$IMD1004=="NZL_1996"]=33.8#recoding NZL_1996 percent vote
d1$IMD5001_B[d1$IMD1004=="NZL_1996"]=28.2#(they coded LH instead than PL percent vote)
d1$IMD5001_C[d1$IMD1004=="NZL_1996"]=13.3
d1$IMD5001_D[d1$IMD1004=="NZL_1996"]=10.1
d1$IMD5001_E[d1$IMD1004=="NZL_1996"]=6.1
d1$IMD5001_F[d1$IMD1004=="NZL_1996"]=4.3
#Norway:#
d1$NP[d1$IMD1004=="NOR_1997"]=6
d1$NP[d1$IMD1004=="NOR_2005"]=6
d1$NP[d1$IMD1004=="NOR_2009"]=6
d1$NP[d1$IMD1004=="NOR_2013"]=6
d5$NP[d5$E1004=="NOR_2017"]=6#
#Peru:#
d1$CoalCode[d1$IMD1004=="PER_2011"]=1
d1$CoalCode[d1$IMD1004=="PER_2016"]=1
d1$NP[d1$IMD1004=="PER_2001"]=6
d1$NP[d1$IMD1004=="PER_2011"]=5
d1$NP[d1$IMD1004=="PER_2016"]=6
d1$IMD5002_B[d1$IMD1004=="PER_2001"]=23.3#recoding PER_2011 percent seats (error)
d1$IMD3008_F[d1$IMD1004=="PER_2011"]=NA#party likability: F leader not coded
d1$IMD3009_F[d1$IMD1004=="PER_2011"]=NA#leader likability: F leader not coded
d1$IMD3007_F[d1$IMD1004=="PER_2011"]=NA#LRP: F leader not coded
#Poland:#
d1$NP[d1$IMD1004=="POL_1997"]=6
d1$NP[d1$IMD1004=="POL_2005"]=6
d1$NP[d1$IMD1004=="POL_2007"]=4
d1$NP[d1$IMD1004=="POL_2011"]=5
d1$IMD3008_F[d1$IMD1004=="POL_2005"]=d1$IMD3008_H[d1$IMD1004=="POL_2005"]#party likability: F not coded, substituted with H
d1$IMD3009_F[d1$IMD1004=="POL_2005"]=d1$IMD3009_H[d1$IMD1004=="POL_2005"]#leader likability: F not coded, substituted with H
d1$IMD3007_F[d1$IMD1004=="POL_2005"]=d1$IMD3007_H[d1$IMD1004=="POL_2005"]#LRP: F not coded, substituted with H
#Portugal:#
d1$NP[d1$IMD1004=="PRT_2002"]=6
d1$NP[d1$IMD1004=="PRT_2009"]=5
d1$NP[d1$IMD1004=="PRT_2015"]=5
d1$IMD5001_A[d1$IMD1004=="PRT_2002"]=40.2#recoding percent vote (error)
d1$IMD5001_B[d1$IMD1004=="PRT_2002"]=37.8
d1$IMD3008_E[d1$IMD1004=="PRT_2015"]=d1$IMD3008_F[d1$IMD1004=="PRT_2015"]#party likability
d1$IMD3008_F[d1$IMD1004=="PRT_2015"]=NA#scale up letters since _E not coded
d1$IMD3009_E[d1$IMD1004=="PRT_2015"]=d1$IMD3009_F[d1$IMD1004=="PRT_2015"]#leader likability
d1$IMD3009_F[d1$IMD1004=="PRT_2015"]=NA#scale up letters since _E not coded
d1$IMD3007_E[d1$IMD1004=="PRT_2015"]=d1$IMD3007_F[d1$IMD1004=="PRT_2015"]#LRP
d1$IMD3007_F[d1$IMD1004=="PRT_2015"]=NA#scale up letters since _E not coded
#Serbia:#
d1$NP[d1$IMD1004=="SRB_2012"]=6
#Slovakia:#
d1$NP[d1$IMD1004=="SVK_2010"]=6
d1$NP[d1$IMD1004=="SVK_2016"]=6
#Slovenia:#
d1$NP[d1$IMD1004=="SVN_1996"]=6
d1$NP[d1$IMD1004=="SVN_2008"]=6
d1$NP[d1$IMD1004=="SVN_2011"]=6
d1$IMD5002_A[d1$IMD1004=="SVN_2011"]=31.1#recoding SVN_2011 percent seats (error)
d1$IMD5002_B[d1$IMD1004=="SVN_2011"]=28.9
d1$IMD5002_C[d1$IMD1004=="SVN_2011"]=11.1
d1$IMD5002_D[d1$IMD1004=="SVN_2011"]=8.9
d1$IMD5002_E[d1$IMD1004=="SVN_2011"]=6.7
d1$IMD5002_F[d1$IMD1004=="SVN_2011"]=6.7
d1$IMD3008_F[d1$IMD1004=="SVN_1996"]=d1$IMD3008_G[d1$IMD1004=="SVN_1996"]#party likability: F not coded, substituted with G
d1$IMD3009_F[d1$IMD1004=="SVN_1996"]=d1$IMD3009_G[d1$IMD1004=="SVN_1996"]#leader likability: F not coded, substituted with G
d1$IMD3007_F[d1$IMD1004=="SVN_1996"]=d1$IMD3007_G[d1$IMD1004=="SVN_1996"]#LRP: F not coded, substituted with G
#South Africa:#
d1$NP[d1$IMD1004=="ZAF_2009"]=6
d1$NP[d1$IMD1004=="ZAF_2014"]=6
#Spain:#
d1$NP[d1$IMD1004=="ESP_1996"]=5#no data for _F
d1$NP[d1$IMD1004=="ESP_2000"]=5#no data for _F
d1$NP[d1$IMD1004=="ESP_2008"]=6
d11$A3020_D[d11$A1004=="ESP_1996"&d11$A2019!=5]=NA#set party like Convergence and Union as NA outside Cataluna
d1$IMD3008_D[d1$IMD1004=="ESP_1996"]=d11$A3020_D[d11$A1004=="ESP_1996"]
d11$A3020_D[d11$A1004=="ESP_2000"&d11$A2019!=5]=NA
d1$IMD3008_D[d1$IMD1004=="ESP_2000"]=d11$A3020_D[d11$A1004=="ESP_2000"]
d13$C3009_C[d13$C1004=="ESP_2008"&d13$C2027!=5]=NA
d1$IMD3008_C[d1$IMD1004=="ESP_2008"]=d13$C3009_C[d13$C1004=="ESP_2008"]
d11$A3020_E[d11$A1004=="ESP_1996"&!(d11$A2019%in%c(15,16))]=NA#set party like Basque Nationalist Party as NA outside Navarra and Pais Vasco
d1$IMD3008_E[d1$IMD1004=="ESP_1996"]=d11$A3020_E[d11$A1004=="ESP_1996"]
d11$A3020_E[d11$A1004=="ESP_2000"&!(d11$A2019%in%c(15,16))]=NA
d1$IMD3008_E[d1$IMD1004=="ESP_2000"]=d11$A3020_E[d11$A1004=="ESP_2000"]
d13$C3009_D[d13$C1004=="ESP_2008"&!(d13$C2027%in%c(15,16))]=NA
d1$IMD3008_D[d1$IMD1004=="ESP_2008"]=d13$C3009_D[d13$C1004=="ESP_2008"]
d13$C3009_E[d13$C1004=="ESP_2008"&!(d13$C2027%in%c(5,17))]=NA#set party like Republican Left of Catalonia as NA outside Cataluna and Comunidad Valenciana
d1$IMD3008_E[d1$IMD1004=="ESP_2008"]=d13$C3009_E[d13$C1004=="ESP_2008"]
d11$A3021_D[d11$A1004=="ESP_1996"&d11$A2019!=5]=NA#set leader like Convergence and Union as NA outside Cataluna
d1$IMD3009_D[d1$IMD1004=="ESP_1996"]=d11$A3021_D[d11$A1004=="ESP_1996"]
d11$A3021_D[d11$A1004=="ESP_2000"&d11$A2019!=5]=NA
d1$IMD3009_D[d1$IMD1004=="ESP_2000"]=d11$A3021_D[d11$A1004=="ESP_2000"]
d13$C3010_C[d13$C1004=="ESP_2008"&d13$C2027!=5]=NA
d1$IMD3009_C[d1$IMD1004=="ESP_2008"]=d13$C3010_C[d13$C1004=="ESP_2008"]
d11$A3021_E[d11$A1004=="ESP_1996"&!(d11$A2019%in%c(15,16))]=NA#set leader like Basque Nationalist Party as NA outside Navarra and Pais Vasco
d1$IMD3009_E[d1$IMD1004=="ESP_1996"]=d11$A3021_E[d11$A1004=="ESP_1996"]
d11$A3021_E[d11$A1004=="ESP_2000"&!(d11$A2019%in%c(15,16))]=NA
d1$IMD3009_E[d1$IMD1004=="ESP_2000"]=d11$A3021_E[d11$A1004=="ESP_2000"]
d13$C3010_D[d13$C1004=="ESP_2008"&!(d13$C2027%in%c(15,16))]=NA
d1$IMD3009_D[d1$IMD1004=="ESP_2008"]=d13$C3010_D[d13$C1004=="ESP_2008"]
d13$C3010_E[d13$C1004=="ESP_2008"&!(d13$C2027%in%c(5,17))]=NA#set leader like Republican Left of Catalonia as NA outside Cataluna and Comunidad Valenciana
d1$IMD3009_E[d1$IMD1004=="ESP_2008"]=d13$C3010_E[d13$C1004=="ESP_2008"]
d11$A3032_D[d11$A1004=="ESP_1996"&d11$A2019!=5]=NA#set LRP Convergence and Union as NA outside Cataluna
d1$IMD3007_D[d1$IMD1004=="ESP_1996"]=d11$A3032_D[d11$A1004=="ESP_1996"]
d11$A3032_D[d11$A1004=="ESP_2000"&d11$A2019!=5]=NA
d1$IMD3007_D[d1$IMD1004=="ESP_2000"]=d11$A3032_D[d11$A1004=="ESP_2000"]
d13$C3011_C[d13$C1004=="ESP_2008"&d13$C2027!=5]=NA
d1$IMD3007_C[d1$IMD1004=="ESP_2008"]=d13$C3011_C[d13$C1004=="ESP_2008"]
d11$A3032_E[d11$A1004=="ESP_1996"&!(d11$A2019%in%c(15,16))]=NA#set LRP Basque Nationalist Party as NA outside Navarra and Pais Vasco
d1$IMD3007_E[d1$IMD1004=="ESP_1996"]=d11$A3032_E[d11$A1004=="ESP_1996"]
d11$A3032_E[d11$A1004=="ESP_2000"&!(d11$A2019%in%c(15,16))]=NA
d1$IMD3007_E[d1$IMD1004=="ESP_2000"]=d11$A3032_E[d11$A1004=="ESP_2000"]
d13$C3011_D[d13$C1004=="ESP_2008"&!(d13$C2027%in%c(15,16))]=NA
d1$IMD3007_D[d1$IMD1004=="ESP_2008"]=d13$C3011_D[d13$C1004=="ESP_2008"]
d13$C3011_E[d13$C1004=="ESP_2008"&!(d13$C2027%in%c(5,17))]=NA#set LRP Republican Left of Catalonia as NA outside Cataluna and Comunidad Valenciana
d1$IMD3007_E[d1$IMD1004=="ESP_2008"]=d13$C3011_E[d13$C1004=="ESP_2008"]
#Sweden:#
d1$NP[d1$IMD1004=="SWE_1998"]=6
d1$NP[d1$IMD1004=="SWE_2006"]=6
d1$NP[d1$IMD1004=="SWE_2014"]=6
d1$IMD5002_A[d1$IMD1004=="SWE_1998"]=37.5#recoding SWE_1998 percent seats (error)
#Taiwan:#
d1$NP[d1$IMD1004=="TWN_1996"]=3


#D1----
#Election variables:
colnames(d1)[colnames(d1)=="IMD1001"]="Ed"#Election Dataset
d1$Ed[d1$IMD1008_MOD_1==1]="CSES-MODULE-1"
d1$Ed[d1$IMD1008_MOD_2==1]="CSES-MODULE-2"#Note: PRT_2002 is assigned to both modules 1 and 2, I'm assigning it to only module 2 bc of year
d1$Ed[d1$IMD1008_MOD_3==1]="CSES-MODULE-3"
d1$Ed[d1$IMD1008_MOD_4==1]="CSES-MODULE-4"
colnames(d1)[colnames(d1)=="IMD1004"]="Es"#Election Study
d1$Ec=substr(d1$Es, 1, 3)#Election Country
colnames(d1)[colnames(d1)=="IMD1008_YEAR"]="Ey"#Election Year
colnames(d1)[colnames(d1)=="IMD1005"]="Rid"#Respondent id
colnames(d1)[colnames(d1)=="IMD2001_1"]="Rage"#Respondent Age
d1$Rage[d1$Rage>115]=NA
d1$Rwd=1#Respondent Weight Demographic:
d1$Rwd[d1$Es=="CAN_1997"]=d1$IMD1010_1[d1$Es=="CAN_1997"]
d1$Rwd[d1$Es=="DNK_1998"]=d1$IMD1010_2[d1$Es=="DNK_1998"]
d1$Rwd[d1$Es=="ESP_1996"]=d1$IMD1010_2[d1$Es=="ESP_1996"]
d1$Rwd[d1$Es=="ESP_2000"]=d1$IMD1010_2[d1$Es=="ESP_2000"]
d1$Rwd[d1$Es=="ESP_2000"&d1$Rwd==0]=1#missing weight put to neutral weight of 1
d1$Rwd[d1$Es=="GBR_1997"]=d1$IMD1010_1[d1$Es=="GBR_1997"]
d1$Rwd[d1$Es=="NZL_1996"]=d1$IMD1010_1[d1$Es=="NZL_1996"]
d1$Rwd[d1$Es=="CAN_2008"]=d1$IMD1010_1[d1$Es=="CAN_2008"]
d1$Rwd[d1$Es=="DEU_2005"]=d1$IMD1010_2[d1$Es=="DEU_2005"]
d1$Rwd[d1$Es=="DNK_2007"]=d1$IMD1010_1[d1$Es=="DNK_2007"]
d1$Rwd[d1$Es=="ESP_2008"]=d1$IMD1010_2[d1$Es=="ESP_2008"]
d1$Rwd[d1$Es=="IRL_2007"]=d1$IMD1010_2[d1$Es=="IRL_2007"]
d1$Rwd[d1$Es=="NLD_2006"]=d1$IMD1010_2[d1$Es=="NLD_2006"]
d1$Rwd[d1$Es=="NLD_2010"]=d1$IMD1010_2[d1$Es=="NLD_2010"]
d1$Rwd[d1$Es=="NZL_2008"]=d1$IMD1010_2[d1$Es=="NZL_2008"]
d1$Rwd[d1$Es=="AUS_2013"]=d1$IMD1010_2[d1$Es=="AUS_2013"]
d1$Rwd[d1$Es=="AUT_2013"]=d1$IMD1010_2[d1$Es=="AUT_2013"]
d1$Rwd[d1$Es=="CAN_2011"]=d1$IMD1010_1[d1$Es=="CAN_2011"]
d1$Rwd[d1$Es=="CAN_2015"]=d1$IMD1010_1[d1$Es=="CAN_2015"]
d1$Rwd[d1$Es=="FIN_2015"]=d1$IMD1010_2[d1$Es=="FIN_2015"]#also political
d1$Rwd[d1$Es=="GBR_2015"]=d1$IMD1010_2[d1$Es=="GBR_2015"]
d1$Rwd[d1$Es=="GRC_2015"]=d1$IMD1010_2[d1$Es=="GRC_2015"]#also political
d1$Rwd[d1$Es=="NOR_2013"]=d1$IMD1010_2[d1$Es=="NOR_2013"]
d1$Rwd[d1$Es=="NZL_2011"]=d1$IMD1010_2[d1$Es=="NZL_2011"]
d1$Rwd[d1$Es=="NZL_2014"]=d1$IMD1010_2[d1$Es=="NZL_2014"]
d1$Rwd[d1$Es=="PRT_2015"]=d1$IMD1010_2[d1$Es=="PRT_2015"]
#Vote and party/leader features:
colnames(d1)[colnames(d1)=="IMD3001_LH"]="Vt"#Vote Turnout (0=non voted; 1=voted) TURNOUT - CURRENT LOWER HOUSE ELECTION
d1$Vt[d1$Vt>1]=NA#coding missing as NAs
colnames(d1)[colnames(d1)=="IMD3002_LH_PL"]="Vn"#Vote Numerical CURRENT LOWER HOUSE ELECTION: VOTE CHOICE - PARTY LIST
d1$Vn[d1$Es=="AUS_1996"]=d1$IMD3002_LH_DC[d1$Es=="AUS_1996"]#assigning LH_DC to Vn for relevant elections
d1$Vn[d1$Es=="AUS_2004"]=d1$IMD3002_LH_DC[d1$Es=="AUS_2004"]
d1$Vn[d1$Es=="AUS_2007"]=d1$IMD3002_LH_DC[d1$Es=="AUS_2007"]
d1$Vn[d1$Es=="AUS_2013"]=d1$IMD3002_LH_DC[d1$Es=="AUS_2013"]
d1$Vn[d1$Es=="CAN_1997"]=d1$IMD3002_LH_DC[d1$Es=="CAN_1997"]
d1$Vn[d1$Es=="CAN_2008"]=d1$IMD3002_LH_DC[d1$Es=="CAN_2008"]
d1$Vn[d1$Es=="CAN_2011"]=d1$IMD3002_LH_DC[d1$Es=="CAN_2011"]
d1$Vn[d1$Es=="CAN_2015"]=d1$IMD3002_LH_DC[d1$Es=="CAN_2015"]
d1$Vn[d1$Es=="FRA_2007"]=d1$IMD3002_LH_DC[d1$Es=="FRA_2007"]
d1$Vn[d1$Es=="GBR_1997"]=d1$IMD3002_LH_DC[d1$Es=="GBR_1997"]
d1$Vn[d1$Es=="GBR_2015"]=d1$IMD3002_LH_DC[d1$Es=="GBR_2015"]
d1$Vn[d1$Es=="IRL_2007"]=d1$IMD3002_LH_DC[d1$Es=="IRL_2007"]
d1$Vn[d1$Es=="IRL_2011"]=d1$IMD3002_LH_DC[d1$Es=="IRL_2011"]
d1$Vn[d1$Es=="MEX_2000"]=d1$IMD3002_LH_DC[d1$Es=="MEX_2000"]
d1$Vn[d1$Es=="MEX_2006"]=d1$IMD3002_LH_DC[d1$Es=="MEX_2006"]
d1$Vn[d1$Es=="MEX_2009"]=d1$IMD3002_LH_DC[d1$Es=="MEX_2009"]
d1$Vn[d1$Es=="TWN_1996"]=d1$IMD3002_LH_DC[d1$Es=="TWN_1996"]

d1$Va=NA#Vote Alphabetical: code vote from numerical (000001-9000000) to alphabetical modified (1:6):
#d1 assign alphabetical modified codes instead than numerical codes----
d1$Va[d1$Es=="ARG_2015"&d1$Vn==0320001]=1#
d1$Va[d1$Es=="ARG_2015"&d1$Vn==0320002]=2
d1$Va[d1$Es=="ARG_2015"&d1$Vn==0320003]=3
d1$Va[d1$Es=="ARG_2015"&d1$Vn==0320004]=4
d1$Va[d1$Es=="ARG_2015"&d1$Vn==0320005]=5
d1$Va[d1$Es=="ARG_2015"&d1$Vn==0320006]=6

d1$Va[d1$Es=="AUS_1996"&d1$Vn==0360003]=1#
d1$Va[d1$Es=="AUS_1996"&d1$Vn==0360001]=2
d1$Va[d1$Es=="AUS_1996"&d1$Vn==0360002]=3
d1$Va[d1$Es=="AUS_1996"&d1$Vn==0360004]=4

d1$Va[d1$Es=="AUS_2004"&d1$Vn==0360001]=1#scaled up since E not coded
d1$Va[d1$Es=="AUS_2004"&d1$Vn==0360003]=2
d1$Va[d1$Es=="AUS_2004"&d1$Vn==0360005]=3
d1$Va[d1$Es=="AUS_2004"&d1$Vn==0360002]=4
d1$Va[d1$Es=="AUS_2004"&d1$Vn==0360004]=5
d1$Va[d1$Es=="AUS_2004"&d1$Vn==0360008]=6

d1$Va[d1$Es=="AUS_2007"&d1$Vn==0360003]=1#
d1$Va[d1$Es=="AUS_2007"&d1$Vn==0360001]=2
d1$Va[d1$Es=="AUS_2007"&d1$Vn==0360002]=3
d1$Va[d1$Es=="AUS_2007"&d1$Vn==0360005]=4

d1$Va[d1$Es=="AUS_2013"&d1$Vn==0360001]=1#scaled up E since D not coded
d1$Va[d1$Es=="AUS_2013"&d1$Vn==0360003]=2
d1$Va[d1$Es=="AUS_2013"&d1$Vn==0360005]=3
d1$Va[d1$Es=="AUS_2013"&d1$Vn==0360002]=4

d1$Va[d1$Es=="AUT_2013"&d1$Vn==0400001]=1#
d1$Va[d1$Es=="AUT_2013"&d1$Vn==0400002]=2
d1$Va[d1$Es=="AUT_2013"&d1$Vn==0400003]=3
d1$Va[d1$Es=="AUT_2013"&d1$Vn==0400005]=4
d1$Va[d1$Es=="AUT_2013"&d1$Vn==0400004]=5
d1$Va[d1$Es=="AUT_2013"&d1$Vn==0400010]=6

d1$Va[d1$Es=="BGR_2014"&d1$Vn==1000001]=1#
d1$Va[d1$Es=="BGR_2014"&d1$Vn==1000020]=2
d1$Va[d1$Es=="BGR_2014"&d1$Vn==1000003]=3
d1$Va[d1$Es=="BGR_2014"&d1$Vn==1000004]=4
d1$Va[d1$Es=="BGR_2014"&d1$Vn==1000005]=5
d1$Va[d1$Es=="BGR_2014"&d1$Vn==1000006]=6

d1$Va[d1$Es=="CAN_1997"&d1$Vn==1240001]=1#
d1$Va[d1$Es=="CAN_1997"&d1$Vn==1240007]=2
d1$Va[d1$Es=="CAN_1997"&d1$Vn==1240006]=3
d1$Va[d1$Es=="CAN_1997"&d1$Vn==1240003]=4
d1$Va[d1$Es=="CAN_1997"&d1$Vn==1240004]=5

d1$Va[d1$Es=="CAN_2008"&d1$Vn==1240002]=1#
d1$Va[d1$Es=="CAN_2008"&d1$Vn==1240001]=2
d1$Va[d1$Es=="CAN_2008"&d1$Vn==1240004]=3
d1$Va[d1$Es=="CAN_2008"&d1$Vn==1240003]=4
d1$Va[d1$Es=="CAN_2008"&d1$Vn==1240005]=5

d1$Va[d1$Es=="CAN_2011"&d1$Vn==1240002]=1#
d1$Va[d1$Es=="CAN_2011"&d1$Vn==1240003]=2
d1$Va[d1$Es=="CAN_2011"&d1$Vn==1240001]=3
d1$Va[d1$Es=="CAN_2011"&d1$Vn==1240004]=4
d1$Va[d1$Es=="CAN_2011"&d1$Vn==1240005]=5

d1$Va[d1$Es=="CAN_2015"&d1$Vn==1240001]=1#
d1$Va[d1$Es=="CAN_2015"&d1$Vn==1240002]=2
d1$Va[d1$Es=="CAN_2015"&d1$Vn==1240003]=3
d1$Va[d1$Es=="CAN_2015"&d1$Vn==1240004]=4
d1$Va[d1$Es=="CAN_2015"&d1$Vn==1240005]=5

d1$Va[d1$Es=="CZE_1996"&d1$Vn==2030002]=1#
d1$Va[d1$Es=="CZE_1996"&d1$Vn==2030001]=2
d1$Va[d1$Es=="CZE_1996"&d1$Vn==2030004]=3
d1$Va[d1$Es=="CZE_1996"&d1$Vn==2030003]=4
d1$Va[d1$Es=="CZE_1996"&d1$Vn==2030018]=5
d1$Va[d1$Es=="CZE_1996"&d1$Vn==2030011]=6

d1$Va[d1$Es=="CZE_2006"&d1$Vn==2030002]=1#
d1$Va[d1$Es=="CZE_2006"&d1$Vn==2030001]=2
d1$Va[d1$Es=="CZE_2006"&d1$Vn==2030004]=3
d1$Va[d1$Es=="CZE_2006"&d1$Vn==2030003]=4
d1$Va[d1$Es=="CZE_2006"&d1$Vn==2030005]=5
d1$Va[d1$Es=="CZE_2006"&d1$Vn==2030074]=6

d1$Va[d1$Es=="CZE_2010"&d1$Vn==2030001]=1#
d1$Va[d1$Es=="CZE_2010"&d1$Vn==2030002]=2
d1$Va[d1$Es=="CZE_2010"&d1$Vn==2030010]=3
d1$Va[d1$Es=="CZE_2010"&d1$Vn==2030004]=4
d1$Va[d1$Es=="CZE_2010"&d1$Vn==2030014]=5
d1$Va[d1$Es=="CZE_2010"&d1$Vn==2030003]=6

d1$Va[d1$Es=="CZE_2013"&d1$Vn==2030001]=1#
d1$Va[d1$Es=="CZE_2013"&d1$Vn==2030006]=2
d1$Va[d1$Es=="CZE_2013"&d1$Vn==2030004]=3
d1$Va[d1$Es=="CZE_2013"&d1$Vn==2030010]=4
d1$Va[d1$Es=="CZE_2013"&d1$Vn==2030002]=5
d1$Va[d1$Es=="CZE_2013"&d1$Vn==2030013]=6

d1$Va[d1$Es=="DEU_1998"&d1$Vn==2760004]=1#DEU:scale up letters after removing CSU
d1$Va[d1$Es=="DEU_1998"&d1$Vn==2760001]=2##note: no need to change 2760001 since it is already vote for CDU, or CSU in Bavaria
d1$Va[d1$Es=="DEU_1998"&d1$Vn==2760005]=3
d1$Va[d1$Es=="DEU_1998"&d1$Vn==2760006]=4
d1$Va[d1$Es=="DEU_1998"&d1$Vn==2760007]=5

d1$Va[d1$Es=="DEU_2005"&d1$Vn==2760004]=1#(comes from module 3, not module 2)
d1$Va[d1$Es=="DEU_2005"&d1$Vn==2760001]=2
d1$Va[d1$Es=="DEU_2005"&d1$Vn==2760006]=3
d1$Va[d1$Es=="DEU_2005"&d1$Vn==2760007]=4
d1$Va[d1$Es=="DEU_2005"&d1$Vn==2760005]=5
d1$Va[d1$Es=="DEU_2005"&d1$Vn==2760009]=6

d1$Va[d1$Es=="DEU_2009"&d1$Vn==2760001]=1#
d1$Va[d1$Es=="DEU_2009"&d1$Vn==2760004]=2
d1$Va[d1$Es=="DEU_2009"&d1$Vn==2760006]=3
d1$Va[d1$Es=="DEU_2009"&d1$Vn==2760007]=4
d1$Va[d1$Es=="DEU_2009"&d1$Vn==2760005]=5

d1$Va[d1$Es=="DEU_2013"&d1$Vn==2760001]=1#
d1$Va[d1$Es=="DEU_2013"&d1$Vn==2760004]=2
d1$Va[d1$Es=="DEU_2013"&d1$Vn==2760007]=3
d1$Va[d1$Es=="DEU_2013"&d1$Vn==2760005]=4
d1$Va[d1$Es=="DEU_2013"&d1$Vn==2760006]=5

d1$Va[d1$Es=="DNK_1998"&d1$Vn==2080001]=1#
d1$Va[d1$Es=="DNK_1998"&d1$Vn==2080002]=2
d1$Va[d1$Es=="DNK_1998"&d1$Vn==2080007]=3
d1$Va[d1$Es=="DNK_1998"&d1$Vn==2080004]=4
d1$Va[d1$Es=="DNK_1998"&d1$Vn==2080010]=5
d1$Va[d1$Es=="DNK_1998"&d1$Vn==2080011]=6

d1$Va[d1$Es=="DNK_2007"&d1$Vn==2080002]=1#
d1$Va[d1$Es=="DNK_2007"&d1$Vn==2080001]=2
d1$Va[d1$Es=="DNK_2007"&d1$Vn==2080010]=3
d1$Va[d1$Es=="DNK_2007"&d1$Vn==2080004]=4
d1$Va[d1$Es=="DNK_2007"&d1$Vn==2080007]=5
d1$Va[d1$Es=="DNK_2007"&d1$Vn==2080006]=6

d1$Va[d1$Es=="ESP_1996"&d1$Vn==7240002]=1#
d1$Va[d1$Es=="ESP_1996"&d1$Vn==7240001]=2
d1$Va[d1$Es=="ESP_1996"&d1$Vn==7240004]=3
d1$Va[d1$Es=="ESP_1996"&d1$Vn==7240003]=4
d1$Va[d1$Es=="ESP_1996"&d1$Vn==7240005]=5

d1$Va[d1$Es=="ESP_2000"&d1$Vn==7240002]=1#
d1$Va[d1$Es=="ESP_2000"&d1$Vn==7240001]=2
d1$Va[d1$Es=="ESP_2000"&d1$Vn==7240004]=3
d1$Va[d1$Es=="ESP_2000"&d1$Vn==7240003]=4
d1$Va[d1$Es=="ESP_2000"&d1$Vn==7240005]=5

d1$Va[d1$Es=="ESP_2008"&d1$Vn==7240001]=1#
d1$Va[d1$Es=="ESP_2008"&d1$Vn==7240002]=2
d1$Va[d1$Es=="ESP_2008"&d1$Vn==7240003]=3
d1$Va[d1$Es=="ESP_2008"&d1$Vn==7240005]=4
d1$Va[d1$Es=="ESP_2008"&d1$Vn==7240009]=5
d1$Va[d1$Es=="ESP_2008"&d1$Vn==7240004]=6

d1$Va[d1$Es=="FIN_2007"&d1$Vn==2460001]=1#
d1$Va[d1$Es=="FIN_2007"&d1$Vn==2460003]=2
d1$Va[d1$Es=="FIN_2007"&d1$Vn==2460002]=3
d1$Va[d1$Es=="FIN_2007"&d1$Vn==2460006]=4
d1$Va[d1$Es=="FIN_2007"&d1$Vn==2460005]=5
d1$Va[d1$Es=="FIN_2007"&d1$Vn==2460008]=6

d1$Va[d1$Es=="FIN_2011"&d1$Vn==2460003]=1#
d1$Va[d1$Es=="FIN_2011"&d1$Vn==2460002]=2
d1$Va[d1$Es=="FIN_2011"&d1$Vn==2460004]=3
d1$Va[d1$Es=="FIN_2011"&d1$Vn==2460001]=4
d1$Va[d1$Es=="FIN_2011"&d1$Vn==2460006]=5
d1$Va[d1$Es=="FIN_2011"&d1$Vn==2460005]=6

d1$Va[d1$Es=="FIN_2015"&d1$Vn==2460001]=1#
d1$Va[d1$Es=="FIN_2015"&d1$Vn==2460003]=2
d1$Va[d1$Es=="FIN_2015"&d1$Vn==2460004]=3
d1$Va[d1$Es=="FIN_2015"&d1$Vn==2460002]=4
d1$Va[d1$Es=="FIN_2015"&d1$Vn==2460005]=5
d1$Va[d1$Es=="FIN_2015"&d1$Vn==2460006]=6

d1$Va[d1$Es=="FRA_2007"&d1$Vn==2500001]=1#
d1$Va[d1$Es=="FRA_2007"&d1$Vn==2500002]=2
d1$Va[d1$Es=="FRA_2007"&d1$Vn==2500003]=3
d1$Va[d1$Es=="FRA_2007"&d1$Vn==2500007]=4
d1$Va[d1$Es=="FRA_2007"&d1$Vn==2500004]=5
d1$Va[d1$Es=="FRA_2007"&d1$Vn==2500005]=6

d1$Va[d1$Es=="GBR_1997"&d1$Vn==8260002]=1#
d1$Va[d1$Es=="GBR_1997"&d1$Vn==8260001]=2
d1$Va[d1$Es=="GBR_1997"&d1$Vn==8260003]=3
d1$Va[d1$Es=="GBR_1997"&d1$Vn==8260004]=4
d1$Va[d1$Es=="GBR_1997"&d1$Vn==8260006]=5

d1$Va[d1$Es=="GBR_2015"&d1$Vn==8260001]=1#
d1$Va[d1$Es=="GBR_2015"&d1$Vn==8260002]=2
d1$Va[d1$Es=="GBR_2015"&d1$Vn==8260007]=3
d1$Va[d1$Es=="GBR_2015"&d1$Vn==8260003]=4
d1$Va[d1$Es=="GBR_2015"&d1$Vn==8260004]=5
d1$Va[d1$Es=="GBR_2015"&d1$Vn==8260005]=6

d1$Va[d1$Es=="GRC_2009"&d1$Vn==3000001]=1#
d1$Va[d1$Es=="GRC_2009"&d1$Vn==3000002]=2
d1$Va[d1$Es=="GRC_2009"&d1$Vn==3000004]=3
d1$Va[d1$Es=="GRC_2009"&d1$Vn==3000019]=4
d1$Va[d1$Es=="GRC_2009"&d1$Vn==3000003]=5
d1$Va[d1$Es=="GRC_2009"&d1$Vn==3000005]=6

d1$Va[d1$Es=="GRC_2012"&d1$Vn==3000002]=1#
d1$Va[d1$Es=="GRC_2012"&d1$Vn==3000003]=2
d1$Va[d1$Es=="GRC_2012"&d1$Vn==3000001]=3
d1$Va[d1$Es=="GRC_2012"&d1$Vn==3000007]=4
d1$Va[d1$Es=="GRC_2012"&d1$Vn==3000006]=5
d1$Va[d1$Es=="GRC_2012"&d1$Vn==3000012]=6

d1$Va[d1$Es=="GRC_2015"&d1$Vn==3000003]=1#
d1$Va[d1$Es=="GRC_2015"&d1$Vn==3000002]=2
d1$Va[d1$Es=="GRC_2015"&d1$Vn==3000006]=3
d1$Va[d1$Es=="GRC_2015"&d1$Vn==3000013]=4
d1$Va[d1$Es=="GRC_2015"&d1$Vn==3000004]=5
d1$Va[d1$Es=="GRC_2015"&d1$Vn==3000007]=6

d1$Va[d1$Es=="HRV_2007"&d1$Vn==1910001]=1#
d1$Va[d1$Es=="HRV_2007"&d1$Vn==1910002]=2
d1$Va[d1$Es=="HRV_2007"&d1$Vn==1910003]=3
d1$Va[d1$Es=="HRV_2007"&d1$Vn==1910007]=4
d1$Va[d1$Es=="HRV_2007"&d1$Vn==1910006]=5
d1$Va[d1$Es=="HRV_2007"&d1$Vn==1910010]=6

d1$Va[d1$Es=="IRL_2007"&d1$Vn==3720001]=1#
d1$Va[d1$Es=="IRL_2007"&d1$Vn==3720002]=2
d1$Va[d1$Es=="IRL_2007"&d1$Vn==3720003]=3
d1$Va[d1$Es=="IRL_2007"&d1$Vn==3720005]=4
d1$Va[d1$Es=="IRL_2007"&d1$Vn==3720004]=5
d1$Va[d1$Es=="IRL_2007"&d1$Vn==3720006]=6

d1$Va[d1$Es=="IRL_2011"&d1$Vn==3720002]=1#E not coded, substituted with F
d1$Va[d1$Es=="IRL_2011"&d1$Vn==3720003]=2
d1$Va[d1$Es=="IRL_2011"&d1$Vn==3720001]=3
d1$Va[d1$Es=="IRL_2011"&d1$Vn==3720004]=4
d1$Va[d1$Es=="IRL_2011"&d1$Vn==3720005]=5

d1$Va[d1$Es=="ISL_1999"&d1$Vn==3520001]=1#
d1$Va[d1$Es=="ISL_1999"&d1$Vn==3520002]=2
d1$Va[d1$Es=="ISL_1999"&d1$Vn==3520003]=3
d1$Va[d1$Es=="ISL_1999"&d1$Vn==3520005]=4
d1$Va[d1$Es=="ISL_1999"&d1$Vn==3520004]=5

d1$Va[d1$Es=="ISL_2007"&d1$Vn==3520001]=1#
d1$Va[d1$Es=="ISL_2007"&d1$Vn==3520002]=2
d1$Va[d1$Es=="ISL_2007"&d1$Vn==3520005]=3
d1$Va[d1$Es=="ISL_2007"&d1$Vn==3520003]=4
d1$Va[d1$Es=="ISL_2007"&d1$Vn==3520004]=5
d1$Va[d1$Es=="ISL_2007"&d1$Vn==3520013]=6

d1$Va[d1$Es=="ISL_2009"&d1$Vn==3520002]=1#
d1$Va[d1$Es=="ISL_2009"&d1$Vn==3520001]=2
d1$Va[d1$Es=="ISL_2009"&d1$Vn==3520005]=3
d1$Va[d1$Es=="ISL_2009"&d1$Vn==3520003]=4
d1$Va[d1$Es=="ISL_2009"&d1$Vn==3520009]=5
d1$Va[d1$Es=="ISL_2009"&d1$Vn==3520004]=6

d1$Va[d1$Es=="ISL_2013"&d1$Vn==3520001]=1#
d1$Va[d1$Es=="ISL_2013"&d1$Vn==3520003]=2
d1$Va[d1$Es=="ISL_2013"&d1$Vn==3520002]=3
d1$Va[d1$Es=="ISL_2013"&d1$Vn==3520005]=4
d1$Va[d1$Es=="ISL_2013"&d1$Vn==3520007]=5
d1$Va[d1$Es=="ISL_2013"&d1$Vn==3520008]=6

d1$Va[d1$Es=="ISR_1996"&d1$Vn==3760002]=1#
d1$Va[d1$Es=="ISR_1996"&d1$Vn==3760001]=2
d1$Va[d1$Es=="ISR_1996"&d1$Vn==3760003]=3
d1$Va[d1$Es=="ISR_1996"&d1$Vn==3760014]=4
d1$Va[d1$Es=="ISR_1996"&d1$Vn==3760004]=5

d1$Va[d1$Es=="ISR_2006"&d1$Vn==3760016]=1#
d1$Va[d1$Es=="ISR_2006"&d1$Vn==3760002]=2
d1$Va[d1$Es=="ISR_2006"&d1$Vn==3760003]=3
d1$Va[d1$Es=="ISR_2006"&d1$Vn==3760001]=4
d1$Va[d1$Es=="ISR_2006"&d1$Vn==3760021]=5
d1$Va[d1$Es=="ISR_2006"&d1$Vn==3760050]=6

d1$Va[d1$Es=="ISR_2013"&d1$Vn==3760011]=1#F not coded, substituted with G
d1$Va[d1$Es=="ISR_2013"&d1$Vn==3760007]=2
d1$Va[d1$Es=="ISR_2013"&d1$Vn==3760002]=3
d1$Va[d1$Es=="ISR_2013"&d1$Vn==3760008]=4
d1$Va[d1$Es=="ISR_2013"&d1$Vn==3760003]=5
d1$Va[d1$Es=="ISR_2013"&d1$Vn==3760009]=6

d1$Va[d1$Es=="LVA_2010"&d1$Vn==4280002]=1#
d1$Va[d1$Es=="LVA_2010"&d1$Vn==4280001]=2
d1$Va[d1$Es=="LVA_2010"&d1$Vn==4280005]=3
d1$Va[d1$Es=="LVA_2010"&d1$Vn==4280003]=4
d1$Va[d1$Es=="LVA_2010"&d1$Vn==4280004]=5
d1$Va[d1$Es=="LVA_2010"&d1$Vn==4280006]=6

d1$Va[d1$Es=="LVA_2011"&d1$Vn==4280001]=1#
d1$Va[d1$Es=="LVA_2011"&d1$Vn==4280007]=2
d1$Va[d1$Es=="LVA_2011"&d1$Vn==4280002]=3
d1$Va[d1$Es=="LVA_2011"&d1$Vn==4280003]=4
d1$Va[d1$Es=="LVA_2011"&d1$Vn==4280005]=5
d1$Va[d1$Es=="LVA_2011"&d1$Vn==4280036]=6

d1$Va[d1$Es=="LVA_2014"&d1$Vn==4280001]=1#
d1$Va[d1$Es=="LVA_2014"&d1$Vn==4280002]=2
d1$Va[d1$Es=="LVA_2014"&d1$Vn==4280005]=3
d1$Va[d1$Es=="LVA_2014"&d1$Vn==4280003]=4
d1$Va[d1$Es=="LVA_2014"&d1$Vn==4280013]=5
d1$Va[d1$Es=="LVA_2014"&d1$Vn==4280014]=6

d1$Va[d1$Es=="MEX_2000"&d1$Vn==4840022]=1#
d1$Va[d1$Es=="MEX_2000"&d1$Vn==4840001]=2
d1$Va[d1$Es=="MEX_2000"&d1$Vn==4840023]=3

d1$Va[d1$Es=="MEX_2006"&d1$Vn==4840002]=1#
d1$Va[d1$Es=="MEX_2006"&d1$Vn==4840003]=2
d1$Va[d1$Es=="MEX_2006"&d1$Vn==4840001]=3

d1$Va[d1$Es=="MEX_2009"&d1$Vn==4840001]=1#
d1$Va[d1$Es=="MEX_2009"&d1$Vn==4840002]=2
d1$Va[d1$Es=="MEX_2009"&d1$Vn==4840003]=3

d1$Va[d1$Es=="MNE_2012"&d1$Vn==4990011]=1#
d1$Va[d1$Es=="MNE_2012"&d1$Vn==4990012]=2
d1$Va[d1$Es=="MNE_2012"&d1$Vn==4990003]=3
d1$Va[d1$Es=="MNE_2012"&d1$Vn==4990006]=4
d1$Va[d1$Es=="MNE_2012"&d1$Vn==4990009]=5
d1$Va[d1$Es=="MNE_2012"&d1$Vn==4990037]=6

d1$Va[d1$Es=="NLD_1998"&d1$Vn==5280002]=1#
d1$Va[d1$Es=="NLD_1998"&d1$Vn==5280003]=2
d1$Va[d1$Es=="NLD_1998"&d1$Vn==5280001]=3
d1$Va[d1$Es=="NLD_1998"&d1$Vn==5280004]=4
d1$Va[d1$Es=="NLD_1998"&d1$Vn==5280005]=5
d1$Va[d1$Es=="NLD_1998"&d1$Vn==5280006]=6

d1$Va[d1$Es=="NLD_2006"&d1$Vn==5280001]=1#
d1$Va[d1$Es=="NLD_2006"&d1$Vn==5280002]=2
d1$Va[d1$Es=="NLD_2006"&d1$Vn==5280006]=3
d1$Va[d1$Es=="NLD_2006"&d1$Vn==5280003]=4
d1$Va[d1$Es=="NLD_2006"&d1$Vn==5280009]=5
d1$Va[d1$Es=="NLD_2006"&d1$Vn==5280005]=6

d1$Va[d1$Es=="NLD_2010"&d1$Vn==5280003]=1#
d1$Va[d1$Es=="NLD_2010"&d1$Vn==5280002]=2
d1$Va[d1$Es=="NLD_2010"&d1$Vn==5280009]=3
d1$Va[d1$Es=="NLD_2010"&d1$Vn==5280001]=4
d1$Va[d1$Es=="NLD_2010"&d1$Vn==5280006]=5
d1$Va[d1$Es=="NLD_2010"&d1$Vn==5280004]=6

d1$Va[d1$Es=="NOR_1997"&d1$Vn==5780001]=1#
d1$Va[d1$Es=="NOR_1997"&d1$Vn==5780003]=2
d1$Va[d1$Es=="NOR_1997"&d1$Vn==5780002]=3
d1$Va[d1$Es=="NOR_1997"&d1$Vn==5780007]=4
d1$Va[d1$Es=="NOR_1997"&d1$Vn==5780006]=5
d1$Va[d1$Es=="NOR_1997"&d1$Vn==5780004]=6

d1$Va[d1$Es=="NOR_2005"&d1$Vn==5780001]=1#
d1$Va[d1$Es=="NOR_2005"&d1$Vn==5780003]=2
d1$Va[d1$Es=="NOR_2005"&d1$Vn==5780002]=3
d1$Va[d1$Es=="NOR_2005"&d1$Vn==5780004]=4
d1$Va[d1$Es=="NOR_2005"&d1$Vn==5780007]=5
d1$Va[d1$Es=="NOR_2005"&d1$Vn==5780006]=6

d1$Va[d1$Es=="NOR_2009"&d1$Vn==5780001]=1#
d1$Va[d1$Es=="NOR_2009"&d1$Vn==5780003]=2
d1$Va[d1$Es=="NOR_2009"&d1$Vn==5780002]=3
d1$Va[d1$Es=="NOR_2009"&d1$Vn==5780004]=4
d1$Va[d1$Es=="NOR_2009"&d1$Vn==5780006]=5
d1$Va[d1$Es=="NOR_2009"&d1$Vn==5780007]=6

d1$Va[d1$Es=="NOR_2013"&d1$Vn==5780001]=1#
d1$Va[d1$Es=="NOR_2013"&d1$Vn==5780002]=2
d1$Va[d1$Es=="NOR_2013"&d1$Vn==5780003]=3
d1$Va[d1$Es=="NOR_2013"&d1$Vn==5780007]=4
d1$Va[d1$Es=="NOR_2013"&d1$Vn==5780006]=5
d1$Va[d1$Es=="NOR_2013"&d1$Vn==5780008]=6

d1$Va[d1$Es=="NZL_1996"&d1$Vn==5540001]=1#
d1$Va[d1$Es=="NZL_1996"&d1$Vn==5540002]=2
d1$Va[d1$Es=="NZL_1996"&d1$Vn==5540003]=3
d1$Va[d1$Es=="NZL_1996"&d1$Vn==5540009]=4
d1$Va[d1$Es=="NZL_1996"&d1$Vn==5540004]=5
d1$Va[d1$Es=="NZL_1996"&d1$Vn==5540037]=6

d1$Va[d1$Es=="NZL_2008"&d1$Vn==5540001]=1#
d1$Va[d1$Es=="NZL_2008"&d1$Vn==5540002]=2
d1$Va[d1$Es=="NZL_2008"&d1$Vn==5540005]=3
d1$Va[d1$Es=="NZL_2008"&d1$Vn==5540003]=4
d1$Va[d1$Es=="NZL_2008"&d1$Vn==5540004]=5
d1$Va[d1$Es=="NZL_2008"&d1$Vn==5540008]=6

d1$Va[d1$Es=="NZL_2011"&d1$Vn==5540001]=1#
d1$Va[d1$Es=="NZL_2011"&d1$Vn==5540002]=2
d1$Va[d1$Es=="NZL_2011"&d1$Vn==5540005]=3
d1$Va[d1$Es=="NZL_2011"&d1$Vn==5540003]=4
d1$Va[d1$Es=="NZL_2011"&d1$Vn==5540007]=5
d1$Va[d1$Es=="NZL_2011"&d1$Vn==5540008]=6

d1$Va[d1$Es=="NZL_2014"&d1$Vn==5540001]=1#
d1$Va[d1$Es=="NZL_2014"&d1$Vn==5540002]=2
d1$Va[d1$Es=="NZL_2014"&d1$Vn==5540005]=3
d1$Va[d1$Es=="NZL_2014"&d1$Vn==5540003]=4
d1$Va[d1$Es=="NZL_2014"&d1$Vn==5540007]=5
d1$Va[d1$Es=="NZL_2014"&d1$Vn==5540010]=6

d1$Va[d1$Es=="PER_2001"&d1$Vn==6040008]=1#
d1$Va[d1$Es=="PER_2001"&d1$Vn==6040026]=2
d1$Va[d1$Es=="PER_2001"&d1$Vn==6040052]=3
d1$Va[d1$Es=="PER_2001"&d1$Vn==6040023]=4
d1$Va[d1$Es=="PER_2001"&d1$Vn==6040030]=5
d1$Va[d1$Es=="PER_2001"&d1$Vn==6040027]=6

d1$Va[d1$Es=="PER_2011"&d1$Vn==6040050]=1#
d1$Va[d1$Es=="PER_2011"&d1$Vn==6040001]=2
d1$Va[d1$Es=="PER_2011"&d1$Vn==6040008]=3
d1$Va[d1$Es=="PER_2011"&d1$Vn==6040051]=4
d1$Va[d1$Es=="PER_2011"&d1$Vn==6040015]=5

d1$Va[d1$Es=="PER_2016"&d1$Vn==6040001]=1#
d1$Va[d1$Es=="PER_2016"&d1$Vn==6040002]=2
d1$Va[d1$Es=="PER_2016"&d1$Vn==6040003]=3
d1$Va[d1$Es=="PER_2016"&d1$Vn==6040004]=4
d1$Va[d1$Es=="PER_2016"&d1$Vn==6040005]=5
d1$Va[d1$Es=="PER_2016"&d1$Vn==6040006]=6

d1$Va[d1$Es=="POL_1997"&d1$Vn==6160004]=1#
d1$Va[d1$Es=="POL_1997"&d1$Vn==6160009]=2
d1$Va[d1$Es=="POL_1997"&d1$Vn==6160008]=3
d1$Va[d1$Es=="POL_1997"&d1$Vn==6160003]=4
d1$Va[d1$Es=="POL_1997"&d1$Vn==6160010]=5
d1$Va[d1$Es=="POL_1997"&d1$Vn==6160007]=6

d1$Va[d1$Es=="POL_2005"&d1$Vn==6160002]=1#F not coded, substituted with H
d1$Va[d1$Es=="POL_2005"&d1$Vn==6160001]=2
d1$Va[d1$Es=="POL_2005"&d1$Vn==6160024]=3
d1$Va[d1$Es=="POL_2005"&d1$Vn==6160009]=4
d1$Va[d1$Es=="POL_2005"&d1$Vn==6160014]=5
d1$Va[d1$Es=="POL_2005"&d1$Vn==6160068]=6

d1$Va[d1$Es=="POL_2007"&d1$Vn==6160001]=1#
d1$Va[d1$Es=="POL_2007"&d1$Vn==6160002]=2
d1$Va[d1$Es=="POL_2007"&d1$Vn==6160020]=3
d1$Va[d1$Es=="POL_2007"&d1$Vn==6160003]=4

d1$Va[d1$Es=="POL_2011"&d1$Vn==6160001]=1#
d1$Va[d1$Es=="POL_2011"&d1$Vn==6160002]=2
d1$Va[d1$Es=="POL_2011"&d1$Vn==6160017]=3
d1$Va[d1$Es=="POL_2011"&d1$Vn==6160003]=4
d1$Va[d1$Es=="POL_2011"&d1$Vn==6160009]=5

d1$Va[d1$Es=="PRT_2002"&d1$Vn==6200001]=1#
d1$Va[d1$Es=="PRT_2002"&d1$Vn==6200002]=2
d1$Va[d1$Es=="PRT_2002"&d1$Vn==6200003]=3
d1$Va[d1$Es=="PRT_2002"&d1$Vn==6200004]=4
d1$Va[d1$Es=="PRT_2002"&d1$Vn==6200006]=5
d1$Va[d1$Es=="PRT_2002"&d1$Vn==6200007]=6

d1$Va[d1$Es=="PRT_2009"&d1$Vn==6200002]=1#
d1$Va[d1$Es=="PRT_2009"&d1$Vn==6200001]=2
d1$Va[d1$Es=="PRT_2009"&d1$Vn==6200003]=3
d1$Va[d1$Es=="PRT_2009"&d1$Vn==6200006]=4
d1$Va[d1$Es=="PRT_2009"&d1$Vn==6200004]=5

d1$Va[d1$Es=="PRT_2015"&d1$Vn==6200011]=1#
d1$Va[d1$Es=="PRT_2015"&d1$Vn==6200002]=2
d1$Va[d1$Es=="PRT_2015"&d1$Vn==6200006]=3
d1$Va[d1$Es=="PRT_2015"&d1$Vn==6200004]=4
d1$Va[d1$Es=="PRT_2015"&d1$Vn==6200020]=5

d1$Va[d1$Es=="SRB_2012"&d1$Vn==6880001]=1#
d1$Va[d1$Es=="SRB_2012"&d1$Vn==6880002]=2
d1$Va[d1$Es=="SRB_2012"&d1$Vn==6880004]=3
d1$Va[d1$Es=="SRB_2012"&d1$Vn==6880005]=4
d1$Va[d1$Es=="SRB_2012"&d1$Vn==6880006]=5
d1$Va[d1$Es=="SRB_2012"&d1$Vn==6880007]=6

d1$Va[d1$Es=="SVK_2010"&d1$Vn==7030001]=1#
d1$Va[d1$Es=="SVK_2010"&d1$Vn==7030003]=2
d1$Va[d1$Es=="SVK_2010"&d1$Vn==7030002]=3
d1$Va[d1$Es=="SVK_2010"&d1$Vn==7030004]=4
d1$Va[d1$Es=="SVK_2010"&d1$Vn==7030008]=5
d1$Va[d1$Es=="SVK_2010"&d1$Vn==7030006]=6

d1$Va[d1$Es=="SVK_2016"&d1$Vn==7030001]=1#
d1$Va[d1$Es=="SVK_2016"&d1$Vn==7030002]=2
d1$Va[d1$Es=="SVK_2016"&d1$Vn==7030009]=3
d1$Va[d1$Es=="SVK_2016"&d1$Vn==7030006]=4
d1$Va[d1$Es=="SVK_2016"&d1$Vn==7030012]=5
d1$Va[d1$Es=="SVK_2016"&d1$Vn==7030014]=6

d1$Va[d1$Es=="SVN_1996"&d1$Vn==7050004]=1#F not coded, substituted with G
d1$Va[d1$Es=="SVN_1996"&d1$Vn==7050006]=2
d1$Va[d1$Es=="SVN_1996"&d1$Vn==7050001]=3
d1$Va[d1$Es=="SVN_1996"&d1$Vn==7050017]=4
d1$Va[d1$Es=="SVN_1996"&d1$Vn==7050002]=5
d1$Va[d1$Es=="SVN_1996"&d1$Vn==7050008]=6

d1$Va[d1$Es=="SVN_2008"&d1$Vn==7050002]=1#
d1$Va[d1$Es=="SVN_2008"&d1$Vn==7050001]=2
d1$Va[d1$Es=="SVN_2008"&d1$Vn==7050020]=3
d1$Va[d1$Es=="SVN_2008"&d1$Vn==7050003]=4
d1$Va[d1$Es=="SVN_2008"&d1$Vn==7050008]=5
d1$Va[d1$Es=="SVN_2008"&d1$Vn==7050019]=6

d1$Va[d1$Es=="SVN_2011"&d1$Vn==7050016]=1#
d1$Va[d1$Es=="SVN_2011"&d1$Vn==7050001]=2
d1$Va[d1$Es=="SVN_2011"&d1$Vn==7050002]=3
d1$Va[d1$Es=="SVN_2011"&d1$Vn==7050009]=4
d1$Va[d1$Es=="SVN_2011"&d1$Vn==7050003]=5
d1$Va[d1$Es=="SVN_2011"&d1$Vn==7050006]=6

d1$Va[d1$Es=="SWE_1998"&d1$Vn==7520001]=1#
d1$Va[d1$Es=="SWE_1998"&d1$Vn==7520002]=2
d1$Va[d1$Es=="SWE_1998"&d1$Vn==7520007]=3
d1$Va[d1$Es=="SWE_1998"&d1$Vn==7520004]=4
d1$Va[d1$Es=="SWE_1998"&d1$Vn==7520006]=5
d1$Va[d1$Es=="SWE_1998"&d1$Vn==7520003]=6

d1$Va[d1$Es=="SWE_2006"&d1$Vn==7520001]=1#
d1$Va[d1$Es=="SWE_2006"&d1$Vn==7520002]=2
d1$Va[d1$Es=="SWE_2006"&d1$Vn==7520006]=3
d1$Va[d1$Es=="SWE_2006"&d1$Vn==7520003]=4
d1$Va[d1$Es=="SWE_2006"&d1$Vn==7520004]=5
d1$Va[d1$Es=="SWE_2006"&d1$Vn==7520007]=6

d1$Va[d1$Es=="SWE_2014"&d1$Vn==7520001]=1#
d1$Va[d1$Es=="SWE_2014"&d1$Vn==7520002]=2
d1$Va[d1$Es=="SWE_2014"&d1$Vn==7520008]=3
d1$Va[d1$Es=="SWE_2014"&d1$Vn==7520005]=4
d1$Va[d1$Es=="SWE_2014"&d1$Vn==7520006]=5
d1$Va[d1$Es=="SWE_2014"&d1$Vn==7520007]=6

d1$Va[d1$Es=="THA_2007"&d1$Vn==7640014]=1
d1$Va[d1$Es=="THA_2007"&d1$Vn==7640002]=2
d1$Va[d1$Es=="THA_2007"&d1$Vn==7640021]=3
d1$Va[d1$Es=="THA_2007"&d1$Vn==7640008]=4
d1$Va[d1$Es=="THA_2007"&d1$Vn==7640060]=5
d1$Va[d1$Es=="THA_2007"&d1$Vn==7640024]=6

d1$Va[d1$Es=="THA_2011"&d1$Vn==7640001]=1
d1$Va[d1$Es=="THA_2011"&d1$Vn==7640002]=2
d1$Va[d1$Es=="THA_2011"&d1$Vn==7640003]=3
d1$Va[d1$Es=="THA_2011"&d1$Vn==7640004]=4
d1$Va[d1$Es=="THA_2011"&d1$Vn==7640005]=5
d1$Va[d1$Es=="THA_2011"&d1$Vn==7640006]=6

d1$Va[d1$Es=="TWN_1996"&d1$Vn==1580002]=1#
d1$Va[d1$Es=="TWN_1996"&d1$Vn==1580001]=2
d1$Va[d1$Es=="TWN_1996"&d1$Vn==1580004]=3

d1$Va[d1$Es=="ZAF_2009"&d1$Vn==7100001]=1#
d1$Va[d1$Es=="ZAF_2009"&d1$Vn==7100002]=2
d1$Va[d1$Es=="ZAF_2009"&d1$Vn==7100008]=3
d1$Va[d1$Es=="ZAF_2009"&d1$Vn==7100004]=4
d1$Va[d1$Es=="ZAF_2009"&d1$Vn==7100022]=5
d1$Va[d1$Es=="ZAF_2009"&d1$Vn==7100006]=6

d1$Va[d1$Es=="ZAF_2014"&d1$Vn==7100001]=1#
d1$Va[d1$Es=="ZAF_2014"&d1$Vn==7100002]=2
d1$Va[d1$Es=="ZAF_2014"&d1$Vn==7100003]=3
d1$Va[d1$Es=="ZAF_2014"&d1$Vn==7100004]=4
d1$Va[d1$Es=="ZAF_2014"&d1$Vn==7100005]=5
d1$Va[d1$Es=="ZAF_2014"&d1$Vn==7100006]=6

colnames(d1)[colnames(d1) %in% paste0("IMD3008_", LETTERS[1:6])]=paste0("Pl.", 1:6)#party likability [0,10] A-F into 1:6----
d1[,names(d1) %in% paste0("Pl.", 1:6)][d1[,names(d1) %in% paste0("Pl.", 1:6)]>10]=NA#coding missing as NAs

colnames(d1)[colnames(d1) %in% paste0("IMD3009_", LETTERS[1:6])]=paste0("Ll.", 1:6)#leader likability [0,10] A-F into 1:6
d1[,names(d1) %in% paste0("Ll.", 1:6)][d1[,names(d1) %in% paste0("Ll.", 1:6)]>10]=NA#coding missing as NAs

colnames(d1)[colnames(d1) %in% paste0("IMD5002_", LETTERS[1:6])]=paste0("Ps.", 1:6)#party seats A-F into 1:6 PERCENT SEATS - LOWER HOUSE
d1[,names(d1) %in% paste0("Ps.", 1:6)][d1[,names(d1) %in% paste0("Ps.", 1:6)]>100]=NA#coding missing as NAs

colnames(d1)[colnames(d1) %in% paste0("IMD5001_", LETTERS[1:6])]=paste0("Pv.", 1:6)#party votes A-F into 1:6 PERCENT VOTE - LOWER HOUSE
d1[,names(d1) %in% paste0("Pv.", 1:6)][d1[,names(d1) %in% paste0("Pv.", 1:6)]>100]=NA#coding missing as NAs

#EEeconS Election Economy Subjective:
d1$IMD3013_1[d1$IMD3013_1>5]=NA#coding missing as NAs
d1$IMD3013_2[d1$IMD3013_2>5]=NA#coding missing as NAs
d1$IMD3013_3[d1$IMD3013_3>5]=NA#coding missing as NAs
d1$EeconS=NA
d1$EeconS=ifelse(d1$IMD3013_1<3, d1$IMD3013_2,
                 (ifelse(d1$IMD3013_1==3, 3,
                         (ifelse(d1$IMD3013_1>3,d1$IMD3013_3,d1$EeconS)))))

#recode EeconS from CSES scale
# 1. GOTTEN BETTER c(1,2,3,4,5)
# 5. GOTTEN WORSE 
# to TEV scale, noting that the TEV used the conversion scale below for same elections:
# 0. Negative evaluation c(0, 0.25, 0.5, 0.75, 1)
# 1. Positive evaluation
d1$EeconS[d1$EeconS==1]=1
d1$EeconS[d1$EeconS==2]=0.75
d1$EeconS[d1$EeconS==3]=0.5
d1$EeconS[d1$EeconS==4]=0.25
d1$EeconS[d1$EeconS==5]=0

#Elections with EeconS:
table(d1$Es,d1$EeconS)
```

```
##           
##               0 0.25  0.5 0.75    1
##   ARG_2015   74  609  505  186   26
##   AUS_1996  368  424  596  259   29
##   AUS_2004    0    0    0    0    0
##   AUS_2007    0    0    0    0    0
##   AUS_2013  370  693 1753  901  151
##   AUT_2013   70  251  525  116   11
##   BGR_2014  194  224  488   64    5
##   CAN_1997   38  189  813  708   93
##   CAN_2008    0    0    0    0    0
##   CAN_2011    0    0 1439    0    0
##   CAN_2015    0    0 1651    0    0
##   CZE_1996  127  202  566  232   32
##   CZE_2006    0    0    0    0    0
##   CZE_2010    0    0    0    0    0
##   CZE_2013  359  365  703   93    6
##   DEU_1998   52  136  482  226   28
##   DEU_2005    0    0    0    0    0
##   DEU_2009    0    0    0    0    0
##   DEU_2013   30  158  729  199   26
##   DNK_1998   11  263  992  526   64
##   DNK_2007    0    0    0    0    0
##   ESP_1996  133  208  656  166   15
##   ESP_2000   30   45  576  412  113
##   ESP_2008    0    0    0    0    0
##   FIN_2007    0    0    0    0    0
##   FIN_2011    0    0    0    0    0
##   FIN_2015  389  621  477   58    6
##   FRA_2007    0    0    0    0    0
##   GBR_1997   63  465 1278  921  129
##   GBR_2015   81  336  465  593   41
##   GRC_2009    0    0    0    0    0
##   GRC_2012  409  326   75   50    6
##   GRC_2015  623  192  163   18    3
##   HRV_2007    0    0    0    0    0
##   IRL_2007    0    0    0    0    0
##   IRL_2011  876  457  260   63   15
##   ISL_1999   32  211  708  485   86
##   ISL_2007    0    0    0    0    0
##   ISL_2009    0    0    0    0    0
##   ISL_2013   46  133  639  523   37
##   ISR_1996  168  182  363  168  101
##   ISR_2006    0    0    0    0    0
##   ISR_2013  226  336  297  107   22
##   LVA_2010    0    0    0    0    0
##   LVA_2011  187  176  488  133    3
##   LVA_2014  123  150  539  181   15
##   MEX_2000  290  198  827  308   69
##   MEX_2006    0    0    0    0    0
##   MEX_2009    0    0    0    0    0
##   MNE_2012  272  214    1   46    8
##   NLD_1998   11   60  825  724  134
##   NLD_2006    0    0    0    0    0
##   NLD_2010    0    0    0    0    0
##   NOR_1997   39    5 1016  658  317
##   NOR_2005    0    0    0    0    0
##   NOR_2009    0    0    0    0    0
##   NOR_2013    9  118 1285  212   61
##   NZL_1996  242  588 1368 1112  221
##   NZL_2008    0    0    0    0    0
##   NZL_2011  257  499  370  197    9
##   NZL_2014   65  175  298  506   98
##   PER_2001    0    0    0    0    0
##   PER_2011    0    0    0    0    0
##   PER_2016  254  333  615  314   42
##   POL_1997  124  186 1180  255   62
##   POL_2005    0    0    0    0    0
##   POL_2007    0    0    0    0    0
##   POL_2011  105  581  805  312   15
##   PRT_2002  461  201  453   38   15
##   PRT_2009    0    0    0    0    0
##   PRT_2015  360  403  505  169   21
##   SRB_2012  483  301  598   80   11
##   SVK_2010    0    0    0    0    0
##   SVK_2016  126  133  711  128   14
##   SVN_1996   70  729  961  135    0
##   SVN_2008    0    0    0    0    0
##   SVN_2011  351  509  117   26    3
##   SWE_1998  100   70  348  352   87
##   SWE_2006    0    0    0    0    0
##   SWE_2014   45   89  419  177   34
##   TWN_1996  440  232  294  138   12
##   ZAF_2009    0    0    0    0    0
##   ZAF_2014  153  353  440  198   74
```

``` r
d1$Eecon=0
d1$Eecon[d1$Es %in% c("ARG_2015","AUS_1996","AUS_2013","AUT_2013","BGR_2014","CAN_1997","CZE_1996","CZE_2013","DEU_1998
                      ","DEU_2013","DNK_1998","ESP_1996","ESP_2000","FIN_2015","GBR_1997","GBR_2015
                      ","GRC_2012","GRC_2015","HUN_1998","IRL_2011","ISL_1999","ISL_2013","ISR_1996","ISR_2013","LVA_2011","LVA_2014","MEX_2000
                      ","MNE_2012","NLD_1998","NOR_1997","NOR_2013","NZL_1996","NZL_2011","NZL_2014
                      ","PER_2016","POL_1997","POL_2011","PRT_2002","PRT_2015","SRB_2012","SVK_2016","SVN_1996","SVN_2011","SWE_1998
                      ","SWE_2014","TWN_1996","ZAF_2014")]=1

#LRR=Left Right Respondent (voter LR self):
d1$LRR=d1$IMD3006
d1$LRR[d1$LRR>10]=NA
#recoding to 0-10 scale: already 0-10 scale
#LRP.i=Left Right Parties:
d1$LRP.1=d1$IMD3007_A#LEFT-RIGHT - PARTY A
d1$LRP.2=d1$IMD3007_B#LEFT-RIGHT - PARTY B
d1$LRP.3=d1$IMD3007_C#LEFT-RIGHT - PARTY C
d1$LRP.4=d1$IMD3007_D#LEFT-RIGHT - PARTY D
d1$LRP.5=d1$IMD3007_E#LEFT-RIGHT - PARTY E
d1$LRP.6=d1$IMD3007_F#LEFT-RIGHT - PARTY F
# d1$LRP.7=as.numeric(NA)
d1$LRP.1[d1$LRP.1>10]=NA
d1$LRP.2[d1$LRP.2>10]=NA
d1$LRP.3[d1$LRP.3>10]=NA
d1$LRP.4[d1$LRP.4>10]=NA
d1$LRP.5[d1$LRP.5>10]=NA
d1$LRP.6[d1$LRP.6>10]=NA
#recoding to 0-10 scale: already 0-10 scale
#Elections with LRR and LRP:
d1$LR=1
d1$LR[d1$Es %in% c("ARG_2015","PER_2016","NZL_2011","MNE_2012","CZE_2006")]=0

#Seats to vote ratios:
d1$P1SVrat=d1$Ps.1/d1$Pv.1#for most voted party
d1$P2SVrat=d1$Ps.2/d1$Pv.2#for second most voted party
d1$P1P2rat=d1$P1SVrat/d1$P2SVrat#ratio of rations first/second party
#Gallagher Disproportionality measure:
d1$GDM=NA
d1$GDM[d1$Es=="AUS_1996"]=10.97
d1$GDM[d1$Es=="AUS_2004"]=8.60
d1$GDM[d1$Es=="AUS_2007"]=10.27
d1$GDM[d1$Es=="AUS_2013"]=9.54
d1$GDM[d1$Es=="AUT_2008"]=2.92
d1$GDM[d1$Es=="AUT_2013"]=3.31
d1$GDM[d1$Es=="BGR_2014"]=2.52
d1$GDM[d1$Es=="CAN_1997"]=13.26
d1$GDM[d1$Es=="CAN_2008"]=10.09
d1$GDM[d1$Es=="CAN_2011"]=12.42
d1$GDM[d1$Es=="CAN_2015"]=12.01
d1$GDM[d1$Es=="CZE_1996"]=5.55
d1$GDM[d1$Es=="CZE_2006"]=5.72
d1$GDM[d1$Es=="CZE_2010"]=8.76
d1$GDM[d1$Es=="CZE_2013"]=6.12
d1$GDM[d1$Es=="DEU_1998"]=3.15#The figure in the effective number of legislative parties (Ns) column is based on 
d1$GDM[d1$Es=="DEU_2005"]=2.16#treating the CDU and CSU as separate parties.
d1$GDM[d1$Es=="DEU_2009"]=3.40#
d1$GDM[d1$Es=="DEU_2013"]=7.83#
d1$GDM[d1$Es=="DNK_1998"]=0.42
d1$GDM[d1$Es=="DNK_2007"]=0.72
d1$GDM[d1$Es=="ESP_1996"]=5.36
d1$GDM[d1$Es=="ESP_2000"]=6.10
d1$GDM[d1$Es=="ESP_2008"]=4.49
d1$GDM[d1$Es=="FIN_2007"]=3.20
d1$GDM[d1$Es=="FIN_2011"]=2.95
d1$GDM[d1$Es=="FIN_2015"]=3.03
d1$GDM[d1$Es=="FRA_2007"]=13.58
d1$GDM[d1$Es=="GBR_1997"]=16.51
d1$GDM[d1$Es=="GBR_2015"]=15.02
d1$GDM[d1$Es=="GRC_2009"]=7.29
d1$GDM[d1$Es=="GRC_2012"]=9.96#Jun
d1$GDM[d1$Es=="GRC_2015"]=9.98#Jan
d1$GDM[d1$Es=="IRL_2007"]=5.85
d1$GDM[d1$Es=="IRL_2011"]=8.69
d1$GDM[d1$Es=="ISL_1999"]=1.06
d1$GDM[d1$Es=="ISL_2007"]=3.49
d1$GDM[d1$Es=="ISL_2009"]=2.58
d1$GDM[d1$Es=="ISL_2013"]=6.23
d1$GDM[d1$Es=="ISR_1996"]=1.65
d1$GDM[d1$Es=="ISR_2006"]=2.49
d1$GDM[d1$Es=="ISR_2013"]=3.09
d1$GDM[d1$Es=="LVA_2010"]=2.80
d1$GDM[d1$Es=="LVA_2011"]=2.76
d1$GDM[d1$Es=="LVA_2014"]=2.30
d1$GDM[d1$Es=="MEX_2000"]=6.70#overall
d1$GDM[d1$Es=="MEX_2006"]=6.34#overall
d1$GDM[d1$Es=="MEX_2009"]=10.46#overall
d1$GDM[d1$Es=="MNE_2012"]=2.28
d1$GDM[d1$Es=="NLD_1998"]=1.28
d1$GDM[d1$Es=="NLD_2006"]=1.03
d1$GDM[d1$Es=="NLD_2010"]=0.81
d1$GDM[d1$Es=="NOR_1997"]=3.44
d1$GDM[d1$Es=="NOR_2005"]=2.67
d1$GDM[d1$Es=="NOR_2009"]=3.01
d1$GDM[d1$Es=="NOR_2013"]=2.56
d1$GDM[d1$Es=="NZL_1996"]=3.43
d1$GDM[d1$Es=="NZL_2008"]=3.84
d1$GDM[d1$Es=="NZL_2011"]=2.38
d1$GDM[d1$Es=="NZL_2014"]=3.72
d1$GDM[d1$Es=="POL_1997"]=10.63
d1$GDM[d1$Es=="POL_2005"]=6.97
d1$GDM[d1$Es=="POL_2007"]=4.67
d1$GDM[d1$Es=="POL_2011"]=5.95
d1$GDM[d1$Es=="PRT_2002"]=4.64
d1$GDM[d1$Es=="PRT_2009"]=5.63
d1$GDM[d1$Es=="PRT_2015"]=5.65
d1$GDM[d1$Es=="ROU_1996"]=6.34
d1$GDM[d1$Es=="SRB_2012"]=6.53
d1$GDM[d1$Es=="SVK_2010"]=7.46
d1$GDM[d1$Es=="SVK_2016"]=6.10
d1$GDM[d1$Es=="SVN_1996"]=3.59
d1$GDM[d1$Es=="SVN_2008"]=3.89
d1$GDM[d1$Es=="SVN_2011"]=3.64
d1$GDM[d1$Es=="SWE_1998"]=0.97
d1$GDM[d1$Es=="SWE_2006"]=3.02
d1$GDM[d1$Es=="SWE_2014"]=2.64
d1$GDM[d1$Es=="TWN_1996"]=#no data!!! check notes
d1$GDM[d1$Es=="ZAF_2009"]=0.30
d1$GDM[d1$Es=="ZAF_2014"]=0.37


#d1 assign CSES numerical ID to alternatives (CSESid.i):----
d1$CSESid.1=NA
d1$CSESid.2=NA
d1$CSESid.3=NA
d1$CSESid.4=NA
d1$CSESid.5=NA
d1$CSESid.6=NA
# d1$CSESid.7=NA

d1$CSESid.1[d1$Es=="ARG_2015"]=0320001
d1$CSESid.2[d1$Es=="ARG_2015"]=0320002
d1$CSESid.3[d1$Es=="ARG_2015"]=0320003
d1$CSESid.4[d1$Es=="ARG_2015"]=0320004
d1$CSESid.5[d1$Es=="ARG_2015"]=0320005
d1$CSESid.6[d1$Es=="ARG_2015"]=0320006

d1$CSESid.1[d1$Es=="AUS_1996"]=0360003
d1$CSESid.2[d1$Es=="AUS_1996"]=0360001
d1$CSESid.3[d1$Es=="AUS_1996"]=0360002
d1$CSESid.4[d1$Es=="AUS_1996"]=0360004

d1$CSESid.1[d1$Es=="AUS_2004"]=0360001#scaled up since E not coded
d1$CSESid.2[d1$Es=="AUS_2004"]=0360003
d1$CSESid.3[d1$Es=="AUS_2004"]=0360005
d1$CSESid.4[d1$Es=="AUS_2004"]=0360002
d1$CSESid.5[d1$Es=="AUS_2004"]=0360004
d1$CSESid.6[d1$Es=="AUS_2004"]=0360008

d1$CSESid.1[d1$Es=="AUS_2007"]=0360003
d1$CSESid.2[d1$Es=="AUS_2007"]=0360001
d1$CSESid.3[d1$Es=="AUS_2007"]=0360002
d1$CSESid.4[d1$Es=="AUS_2007"]=0360005

d1$CSESid.1[d1$Es=="AUS_2013"]=0360001#scaled up E since D not coded
d1$CSESid.2[d1$Es=="AUS_2013"]=0360003
d1$CSESid.3[d1$Es=="AUS_2013"]=0360005
d1$CSESid.4[d1$Es=="AUS_2013"]=0360002

d1$CSESid.1[d1$Es=="AUT_2013"]=0400001
d1$CSESid.2[d1$Es=="AUT_2013"]=0400002
d1$CSESid.3[d1$Es=="AUT_2013"]=0400003
d1$CSESid.4[d1$Es=="AUT_2013"]=0400005
d1$CSESid.5[d1$Es=="AUT_2013"]=0400004
d1$CSESid.6[d1$Es=="AUT_2013"]=0400010

d1$CSESid.1[d1$Es=="BGR_2014"]=1000001
d1$CSESid.2[d1$Es=="BGR_2014"]=1000020
d1$CSESid.3[d1$Es=="BGR_2014"]=1000003
d1$CSESid.4[d1$Es=="BGR_2014"]=1000004
d1$CSESid.5[d1$Es=="BGR_2014"]=1000005
d1$CSESid.6[d1$Es=="BGR_2014"]=1000006

d1$CSESid.1[d1$Es=="CAN_1997"]=1240001
d1$CSESid.2[d1$Es=="CAN_1997"]=1240007
d1$CSESid.3[d1$Es=="CAN_1997"]=1240006
d1$CSESid.4[d1$Es=="CAN_1997"]=1240003
d1$CSESid.5[d1$Es=="CAN_1997"]=1240004

d1$CSESid.1[d1$Es=="CAN_2008"]=1240002
d1$CSESid.2[d1$Es=="CAN_2008"]=1240001
d1$CSESid.3[d1$Es=="CAN_2008"]=1240004
d1$CSESid.4[d1$Es=="CAN_2008"]=1240003
d1$CSESid.5[d1$Es=="CAN_2008"]=1240005

d1$CSESid.1[d1$Es=="CAN_2011"]=1240002
d1$CSESid.2[d1$Es=="CAN_2011"]=1240003
d1$CSESid.3[d1$Es=="CAN_2011"]=1240001
d1$CSESid.4[d1$Es=="CAN_2011"]=1240004
d1$CSESid.5[d1$Es=="CAN_2011"]=1240005

d1$CSESid.1[d1$Es=="CAN_2015"]=1240001
d1$CSESid.2[d1$Es=="CAN_2015"]=1240002
d1$CSESid.3[d1$Es=="CAN_2015"]=1240003
d1$CSESid.4[d1$Es=="CAN_2015"]=1240004
d1$CSESid.5[d1$Es=="CAN_2015"]=1240005

d1$CSESid.1[d1$Es=="CZE_1996"]=2030002
d1$CSESid.2[d1$Es=="CZE_1996"]=2030001
d1$CSESid.3[d1$Es=="CZE_1996"]=2030004
d1$CSESid.4[d1$Es=="CZE_1996"]=2030003
d1$CSESid.5[d1$Es=="CZE_1996"]=2030018
d1$CSESid.6[d1$Es=="CZE_1996"]=2030011

d1$CSESid.1[d1$Es=="CZE_2006"]=2030002
d1$CSESid.2[d1$Es=="CZE_2006"]=2030001
d1$CSESid.3[d1$Es=="CZE_2006"]=2030004
d1$CSESid.4[d1$Es=="CZE_2006"]=2030003
d1$CSESid.5[d1$Es=="CZE_2006"]=2030005
d1$CSESid.6[d1$Es=="CZE_2006"]=2030074

d1$CSESid.1[d1$Es=="CZE_2010"]=2030001
d1$CSESid.2[d1$Es=="CZE_2010"]=2030002
d1$CSESid.3[d1$Es=="CZE_2010"]=2030010
d1$CSESid.4[d1$Es=="CZE_2010"]=2030004
d1$CSESid.5[d1$Es=="CZE_2010"]=2030014
d1$CSESid.6[d1$Es=="CZE_2010"]=2030003

d1$CSESid.1[d1$Es=="CZE_2013"]=2030001
d1$CSESid.2[d1$Es=="CZE_2013"]=2030006
d1$CSESid.3[d1$Es=="CZE_2013"]=2030004
d1$CSESid.4[d1$Es=="CZE_2013"]=2030010
d1$CSESid.5[d1$Es=="CZE_2013"]=2030002
d1$CSESid.6[d1$Es=="CZE_2013"]=2030013

d1$CSESid.1[d1$Es=="DEU_1998"]=2760004#DEU:scale up letters after removing CSU
d1$CSESid.2[d1$Es=="DEU_1998"]=2760001##note: no need to change 2760001 since it is already vote for CDU, or CSU in Bavaria
d1$CSESid.3[d1$Es=="DEU_1998"]=2760005
d1$CSESid.4[d1$Es=="DEU_1998"]=2760006
d1$CSESid.5[d1$Es=="DEU_1998"]=2760007

d1$CSESid.1[d1$Es=="DEU_2005"]=2760004#(comes from module 3, not module 2)
d1$CSESid.2[d1$Es=="DEU_2005"]=2760001
d1$CSESid.3[d1$Es=="DEU_2005"]=2760006
d1$CSESid.4[d1$Es=="DEU_2005"]=2760007
d1$CSESid.5[d1$Es=="DEU_2005"]=2760005
d1$CSESid.6[d1$Es=="DEU_2005"]=2760009

d1$CSESid.1[d1$Es=="DEU_2009"]=2760001
d1$CSESid.2[d1$Es=="DEU_2009"]=2760004
d1$CSESid.3[d1$Es=="DEU_2009"]=2760006
d1$CSESid.4[d1$Es=="DEU_2009"]=2760007
d1$CSESid.5[d1$Es=="DEU_2009"]=2760005

d1$CSESid.1[d1$Es=="DEU_2013"]=2760001
d1$CSESid.2[d1$Es=="DEU_2013"]=2760004
d1$CSESid.3[d1$Es=="DEU_2013"]=2760007
d1$CSESid.4[d1$Es=="DEU_2013"]=2760005
d1$CSESid.5[d1$Es=="DEU_2013"]=2760006

d1$CSESid.1[d1$Es=="DNK_1998"]=2080001
d1$CSESid.2[d1$Es=="DNK_1998"]=2080002
d1$CSESid.3[d1$Es=="DNK_1998"]=2080007
d1$CSESid.4[d1$Es=="DNK_1998"]=2080004
d1$CSESid.5[d1$Es=="DNK_1998"]=2080010
d1$CSESid.6[d1$Es=="DNK_1998"]=2080011

d1$CSESid.1[d1$Es=="DNK_2007"]=2080002
d1$CSESid.2[d1$Es=="DNK_2007"]=2080001
d1$CSESid.3[d1$Es=="DNK_2007"]=2080010
d1$CSESid.4[d1$Es=="DNK_2007"]=2080004
d1$CSESid.5[d1$Es=="DNK_2007"]=2080007
d1$CSESid.6[d1$Es=="DNK_2007"]=2080006

d1$CSESid.1[d1$Es=="ESP_1996"]=7240002
d1$CSESid.2[d1$Es=="ESP_1996"]=7240001
d1$CSESid.3[d1$Es=="ESP_1996"]=7240004
d1$CSESid.4[d1$Es=="ESP_1996"]=7240003
d1$CSESid.5[d1$Es=="ESP_1996"]=7240005

d1$CSESid.1[d1$Es=="ESP_2000"]=7240002
d1$CSESid.2[d1$Es=="ESP_2000"]=7240001
d1$CSESid.3[d1$Es=="ESP_2000"]=7240004
d1$CSESid.4[d1$Es=="ESP_2000"]=7240003
d1$CSESid.5[d1$Es=="ESP_2000"]=7240005

d1$CSESid.1[d1$Es=="ESP_2008"]=7240001
d1$CSESid.2[d1$Es=="ESP_2008"]=7240002
d1$CSESid.3[d1$Es=="ESP_2008"]=7240003
d1$CSESid.4[d1$Es=="ESP_2008"]=7240005
d1$CSESid.5[d1$Es=="ESP_2008"]=7240009
d1$CSESid.6[d1$Es=="ESP_2008"]=7240004

d1$CSESid.1[d1$Es=="FIN_2007"]=2460001
d1$CSESid.2[d1$Es=="FIN_2007"]=2460003
d1$CSESid.3[d1$Es=="FIN_2007"]=2460002
d1$CSESid.4[d1$Es=="FIN_2007"]=2460006
d1$CSESid.5[d1$Es=="FIN_2007"]=2460005
d1$CSESid.6[d1$Es=="FIN_2007"]=2460008

d1$CSESid.1[d1$Es=="FIN_2011"]=2460003
d1$CSESid.2[d1$Es=="FIN_2011"]=2460002
d1$CSESid.3[d1$Es=="FIN_2011"]=2460004
d1$CSESid.4[d1$Es=="FIN_2011"]=2460001
d1$CSESid.5[d1$Es=="FIN_2011"]=2460006
d1$CSESid.6[d1$Es=="FIN_2011"]=2460005

d1$CSESid.1[d1$Es=="FIN_2015"]=2460001
d1$CSESid.2[d1$Es=="FIN_2015"]=2460003
d1$CSESid.3[d1$Es=="FIN_2015"]=2460004
d1$CSESid.4[d1$Es=="FIN_2015"]=2460002
d1$CSESid.5[d1$Es=="FIN_2015"]=2460005
d1$CSESid.6[d1$Es=="FIN_2015"]=2460006

d1$CSESid.1[d1$Es=="FRA_2007"]=2500001
d1$CSESid.2[d1$Es=="FRA_2007"]=2500002
d1$CSESid.3[d1$Es=="FRA_2007"]=2500003
d1$CSESid.4[d1$Es=="FRA_2007"]=2500007
d1$CSESid.5[d1$Es=="FRA_2007"]=2500004
d1$CSESid.6[d1$Es=="FRA_2007"]=2500005

d1$CSESid.1[d1$Es=="GBR_1997"]=8260002
d1$CSESid.2[d1$Es=="GBR_1997"]=8260001
d1$CSESid.3[d1$Es=="GBR_1997"]=8260003
d1$CSESid.4[d1$Es=="GBR_1997"]=8260004
d1$CSESid.5[d1$Es=="GBR_1997"]=8260006

d1$CSESid.1[d1$Es=="GBR_2015"]=8260001
d1$CSESid.2[d1$Es=="GBR_2015"]=8260002
d1$CSESid.3[d1$Es=="GBR_2015"]=8260007
d1$CSESid.4[d1$Es=="GBR_2015"]=8260003
d1$CSESid.5[d1$Es=="GBR_2015"]=8260004
d1$CSESid.6[d1$Es=="GBR_2015"]=8260005

d1$CSESid.1[d1$Es=="GRC_2009"]=3000001
d1$CSESid.2[d1$Es=="GRC_2009"]=3000002
d1$CSESid.3[d1$Es=="GRC_2009"]=3000004
d1$CSESid.4[d1$Es=="GRC_2009"]=3000019
d1$CSESid.5[d1$Es=="GRC_2009"]=3000003
d1$CSESid.6[d1$Es=="GRC_2009"]=3000005

d1$CSESid.1[d1$Es=="GRC_2012"]=3000002
d1$CSESid.2[d1$Es=="GRC_2012"]=3000003
d1$CSESid.3[d1$Es=="GRC_2012"]=3000001
d1$CSESid.4[d1$Es=="GRC_2012"]=3000007
d1$CSESid.5[d1$Es=="GRC_2012"]=3000006
d1$CSESid.6[d1$Es=="GRC_2012"]=3000012

d1$CSESid.1[d1$Es=="GRC_2015"]=3000003
d1$CSESid.2[d1$Es=="GRC_2015"]=3000002
d1$CSESid.3[d1$Es=="GRC_2015"]=3000006
d1$CSESid.4[d1$Es=="GRC_2015"]=3000013
d1$CSESid.5[d1$Es=="GRC_2015"]=3000004
d1$CSESid.6[d1$Es=="GRC_2015"]=3000007

d1$CSESid.1[d1$Es=="HRV_2007"]=1910001
d1$CSESid.2[d1$Es=="HRV_2007"]=1910002
d1$CSESid.3[d1$Es=="HRV_2007"]=1910003
d1$CSESid.4[d1$Es=="HRV_2007"]=1910007
d1$CSESid.5[d1$Es=="HRV_2007"]=1910006
d1$CSESid.6[d1$Es=="HRV_2007"]=1910010

d1$CSESid.1[d1$Es=="IRL_2007"]=3720001
d1$CSESid.2[d1$Es=="IRL_2007"]=3720002
d1$CSESid.3[d1$Es=="IRL_2007"]=3720003
d1$CSESid.4[d1$Es=="IRL_2007"]=3720005
d1$CSESid.5[d1$Es=="IRL_2007"]=3720004
d1$CSESid.6[d1$Es=="IRL_2007"]=3720006

d1$CSESid.1[d1$Es=="IRL_2011"]=3720002#E not coded, substituted with F
d1$CSESid.2[d1$Es=="IRL_2011"]=3720003
d1$CSESid.3[d1$Es=="IRL_2011"]=3720001
d1$CSESid.4[d1$Es=="IRL_2011"]=3720004
d1$CSESid.5[d1$Es=="IRL_2011"]=3720005

d1$CSESid.1[d1$Es=="ISL_1999"]=3520001
d1$CSESid.2[d1$Es=="ISL_1999"]=3520002
d1$CSESid.3[d1$Es=="ISL_1999"]=3520003
d1$CSESid.4[d1$Es=="ISL_1999"]=3520005
d1$CSESid.5[d1$Es=="ISL_1999"]=3520004

d1$CSESid.1[d1$Es=="ISL_2007"]=3520001
d1$CSESid.2[d1$Es=="ISL_2007"]=3520002
d1$CSESid.3[d1$Es=="ISL_2007"]=3520005
d1$CSESid.4[d1$Es=="ISL_2007"]=3520003
d1$CSESid.5[d1$Es=="ISL_2007"]=3520004
d1$CSESid.6[d1$Es=="ISL_2007"]=3520013

d1$CSESid.1[d1$Es=="ISL_2009"]=3520002
d1$CSESid.2[d1$Es=="ISL_2009"]=3520001
d1$CSESid.3[d1$Es=="ISL_2009"]=3520005
d1$CSESid.4[d1$Es=="ISL_2009"]=3520003
d1$CSESid.5[d1$Es=="ISL_2009"]=3520009
d1$CSESid.6[d1$Es=="ISL_2009"]=3520004

d1$CSESid.1[d1$Es=="ISL_2013"]=3520001
d1$CSESid.2[d1$Es=="ISL_2013"]=3520003
d1$CSESid.3[d1$Es=="ISL_2013"]=3520002
d1$CSESid.4[d1$Es=="ISL_2013"]=3520005
d1$CSESid.5[d1$Es=="ISL_2013"]=3520007
d1$CSESid.6[d1$Es=="ISL_2013"]=3520008

d1$CSESid.1[d1$Es=="ISR_1996"]=3760002
d1$CSESid.2[d1$Es=="ISR_1996"]=3760001
d1$CSESid.3[d1$Es=="ISR_1996"]=3760003
d1$CSESid.4[d1$Es=="ISR_1996"]=3760014
d1$CSESid.5[d1$Es=="ISR_1996"]=3760004

d1$CSESid.1[d1$Es=="ISR_2006"]=3760016
d1$CSESid.2[d1$Es=="ISR_2006"]=3760002
d1$CSESid.3[d1$Es=="ISR_2006"]=3760003
d1$CSESid.4[d1$Es=="ISR_2006"]=3760001
d1$CSESid.5[d1$Es=="ISR_2006"]=3760021
d1$CSESid.6[d1$Es=="ISR_2006"]=3760050

d1$CSESid.1[d1$Es=="ISR_2013"]=3760011#F not coded, substituted with G
d1$CSESid.2[d1$Es=="ISR_2013"]=3760007
d1$CSESid.3[d1$Es=="ISR_2013"]=3760002
d1$CSESid.4[d1$Es=="ISR_2013"]=3760008
d1$CSESid.5[d1$Es=="ISR_2013"]=3760003
d1$CSESid.6[d1$Es=="ISR_2013"]=3760009

d1$CSESid.1[d1$Es=="LVA_2010"]=4280002
d1$CSESid.2[d1$Es=="LVA_2010"]=4280001
d1$CSESid.3[d1$Es=="LVA_2010"]=4280005
d1$CSESid.4[d1$Es=="LVA_2010"]=4280003
d1$CSESid.5[d1$Es=="LVA_2010"]=4280004
d1$CSESid.6[d1$Es=="LVA_2010"]=4280006

d1$CSESid.1[d1$Es=="LVA_2011"]=4280001
d1$CSESid.2[d1$Es=="LVA_2011"]=4280007
d1$CSESid.3[d1$Es=="LVA_2011"]=4280002
d1$CSESid.4[d1$Es=="LVA_2011"]=4280003
d1$CSESid.5[d1$Es=="LVA_2011"]=4280005
d1$CSESid.6[d1$Es=="LVA_2011"]=4280036

d1$CSESid.1[d1$Es=="LVA_2014"]=4280001
d1$CSESid.2[d1$Es=="LVA_2014"]=4280002
d1$CSESid.3[d1$Es=="LVA_2014"]=4280005
d1$CSESid.4[d1$Es=="LVA_2014"]=4280003
d1$CSESid.5[d1$Es=="LVA_2014"]=4280013
d1$CSESid.6[d1$Es=="LVA_2014"]=4280014

d1$CSESid.1[d1$Es=="MEX_2000"]=4840022
d1$CSESid.2[d1$Es=="MEX_2000"]=4840001
d1$CSESid.3[d1$Es=="MEX_2000"]=4840023

d1$CSESid.1[d1$Es=="MEX_2006"]=4840002
d1$CSESid.2[d1$Es=="MEX_2006"]=4840003
d1$CSESid.3[d1$Es=="MEX_2006"]=4840001

d1$CSESid.1[d1$Es=="MEX_2009"]=4840001
d1$CSESid.2[d1$Es=="MEX_2009"]=4840002
d1$CSESid.3[d1$Es=="MEX_2009"]=4840003

d1$CSESid.1[d1$Es=="MNE_2012"]=4990011
d1$CSESid.2[d1$Es=="MNE_2012"]=4990012
d1$CSESid.3[d1$Es=="MNE_2012"]=4990003
d1$CSESid.4[d1$Es=="MNE_2012"]=4990006
d1$CSESid.5[d1$Es=="MNE_2012"]=4990009
d1$CSESid.6[d1$Es=="MNE_2012"]=4990037

d1$CSESid.1[d1$Es=="NLD_1998"]=5280002
d1$CSESid.2[d1$Es=="NLD_1998"]=5280003
d1$CSESid.3[d1$Es=="NLD_1998"]=5280001
d1$CSESid.4[d1$Es=="NLD_1998"]=5280004
d1$CSESid.5[d1$Es=="NLD_1998"]=5280005
d1$CSESid.6[d1$Es=="NLD_1998"]=5280006

d1$CSESid.1[d1$Es=="NLD_2006"]=5280001
d1$CSESid.2[d1$Es=="NLD_2006"]=5280002
d1$CSESid.3[d1$Es=="NLD_2006"]=5280006
d1$CSESid.4[d1$Es=="NLD_2006"]=5280003
d1$CSESid.5[d1$Es=="NLD_2006"]=5280009
d1$CSESid.6[d1$Es=="NLD_2006"]=5280005

d1$CSESid.1[d1$Es=="NLD_2010"]=5280003
d1$CSESid.2[d1$Es=="NLD_2010"]=5280002
d1$CSESid.3[d1$Es=="NLD_2010"]=5280009
d1$CSESid.4[d1$Es=="NLD_2010"]=5280001
d1$CSESid.5[d1$Es=="NLD_2010"]=5280006
d1$CSESid.6[d1$Es=="NLD_2010"]=5280004

d1$CSESid.1[d1$Es=="NOR_1997"]=5780001
d1$CSESid.2[d1$Es=="NOR_1997"]=5780003
d1$CSESid.3[d1$Es=="NOR_1997"]=5780002
d1$CSESid.4[d1$Es=="NOR_1997"]=5780007
d1$CSESid.5[d1$Es=="NOR_1997"]=5780006
d1$CSESid.6[d1$Es=="NOR_1997"]=5780004

d1$CSESid.1[d1$Es=="NOR_2005"]=5780001
d1$CSESid.2[d1$Es=="NOR_2005"]=5780003
d1$CSESid.3[d1$Es=="NOR_2005"]=5780002
d1$CSESid.4[d1$Es=="NOR_2005"]=5780004
d1$CSESid.5[d1$Es=="NOR_2005"]=5780007
d1$CSESid.6[d1$Es=="NOR_2005"]=5780006

d1$CSESid.1[d1$Es=="NOR_2009"]=5780001
d1$CSESid.2[d1$Es=="NOR_2009"]=5780003
d1$CSESid.3[d1$Es=="NOR_2009"]=5780002
d1$CSESid.4[d1$Es=="NOR_2009"]=5780004
d1$CSESid.5[d1$Es=="NOR_2009"]=5780006
d1$CSESid.6[d1$Es=="NOR_2009"]=5780007

d1$CSESid.1[d1$Es=="NOR_2013"]=5780001
d1$CSESid.2[d1$Es=="NOR_2013"]=5780002
d1$CSESid.3[d1$Es=="NOR_2013"]=5780003
d1$CSESid.4[d1$Es=="NOR_2013"]=5780007
d1$CSESid.5[d1$Es=="NOR_2013"]=5780006
d1$CSESid.6[d1$Es=="NOR_2013"]=5780008

d1$CSESid.1[d1$Es=="NZL_1996"]=5540001
d1$CSESid.2[d1$Es=="NZL_1996"]=5540002
d1$CSESid.3[d1$Es=="NZL_1996"]=5540003
d1$CSESid.4[d1$Es=="NZL_1996"]=5540009
d1$CSESid.5[d1$Es=="NZL_1996"]=5540004
d1$CSESid.6[d1$Es=="NZL_1996"]=5540037

d1$CSESid.1[d1$Es=="NZL_2008"]=5540001
d1$CSESid.2[d1$Es=="NZL_2008"]=5540002
d1$CSESid.3[d1$Es=="NZL_2008"]=5540005
d1$CSESid.4[d1$Es=="NZL_2008"]=5540003
d1$CSESid.5[d1$Es=="NZL_2008"]=5540004
d1$CSESid.6[d1$Es=="NZL_2008"]=5540008

d1$CSESid.1[d1$Es=="NZL_2011"]=5540001
d1$CSESid.2[d1$Es=="NZL_2011"]=5540002
d1$CSESid.3[d1$Es=="NZL_2011"]=5540005
d1$CSESid.4[d1$Es=="NZL_2011"]=5540003
d1$CSESid.5[d1$Es=="NZL_2011"]=5540007
d1$CSESid.6[d1$Es=="NZL_2011"]=5540008

d1$CSESid.1[d1$Es=="NZL_2014"]=5540001
d1$CSESid.2[d1$Es=="NZL_2014"]=5540002
d1$CSESid.3[d1$Es=="NZL_2014"]=5540005
d1$CSESid.4[d1$Es=="NZL_2014"]=5540003
d1$CSESid.5[d1$Es=="NZL_2014"]=5540007
d1$CSESid.6[d1$Es=="NZL_2014"]=5540010

d1$CSESid.1[d1$Es=="PER_2001"]=6040008
d1$CSESid.2[d1$Es=="PER_2001"]=6040026
d1$CSESid.3[d1$Es=="PER_2001"]=6040052
d1$CSESid.4[d1$Es=="PER_2001"]=6040023
d1$CSESid.5[d1$Es=="PER_2001"]=6040030
d1$CSESid.6[d1$Es=="PER_2001"]=6040027

d1$CSESid.1[d1$Es=="PER_2011"]=6040050
d1$CSESid.2[d1$Es=="PER_2011"]=6040001
d1$CSESid.3[d1$Es=="PER_2011"]=6040008
d1$CSESid.4[d1$Es=="PER_2011"]=6040051
d1$CSESid.5[d1$Es=="PER_2011"]=6040015

d1$CSESid.1[d1$Es=="PER_2016"]=6040001
d1$CSESid.2[d1$Es=="PER_2016"]=6040002
d1$CSESid.3[d1$Es=="PER_2016"]=6040003
d1$CSESid.4[d1$Es=="PER_2016"]=6040004
d1$CSESid.5[d1$Es=="PER_2016"]=6040005
d1$CSESid.6[d1$Es=="PER_2016"]=6040006

d1$CSESid.1[d1$Es=="POL_1997"]=6160004
d1$CSESid.2[d1$Es=="POL_1997"]=6160009
d1$CSESid.3[d1$Es=="POL_1997"]=6160008
d1$CSESid.4[d1$Es=="POL_1997"]=6160003
d1$CSESid.5[d1$Es=="POL_1997"]=6160010
d1$CSESid.6[d1$Es=="POL_1997"]=6160007

d1$CSESid.1[d1$Es=="POL_2005"]=6160002#F not coded, substituted with H
d1$CSESid.2[d1$Es=="POL_2005"]=6160001
d1$CSESid.3[d1$Es=="POL_2005"]=6160024
d1$CSESid.4[d1$Es=="POL_2005"]=6160009
d1$CSESid.5[d1$Es=="POL_2005"]=6160014
d1$CSESid.6[d1$Es=="POL_2005"]=6160068

d1$CSESid.1[d1$Es=="POL_2007"]=6160001
d1$CSESid.2[d1$Es=="POL_2007"]=6160002
d1$CSESid.3[d1$Es=="POL_2007"]=6160020
d1$CSESid.4[d1$Es=="POL_2007"]=6160003

d1$CSESid.1[d1$Es=="POL_2011"]=6160001
d1$CSESid.2[d1$Es=="POL_2011"]=6160002
d1$CSESid.3[d1$Es=="POL_2011"]=6160017
d1$CSESid.4[d1$Es=="POL_2011"]=6160003
d1$CSESid.5[d1$Es=="POL_2011"]=6160009

d1$CSESid.1[d1$Es=="PRT_2002"]=6200001
d1$CSESid.2[d1$Es=="PRT_2002"]=6200002
d1$CSESid.3[d1$Es=="PRT_2002"]=6200003
d1$CSESid.4[d1$Es=="PRT_2002"]=6200004
d1$CSESid.5[d1$Es=="PRT_2002"]=6200006
d1$CSESid.6[d1$Es=="PRT_2002"]=6200007

d1$CSESid.1[d1$Es=="PRT_2009"]=6200002
d1$CSESid.2[d1$Es=="PRT_2009"]=6200001
d1$CSESid.3[d1$Es=="PRT_2009"]=6200003
d1$CSESid.4[d1$Es=="PRT_2009"]=6200006
d1$CSESid.5[d1$Es=="PRT_2009"]=6200004

d1$CSESid.1[d1$Es=="PRT_2015"]=6200011
d1$CSESid.2[d1$Es=="PRT_2015"]=6200002
d1$CSESid.3[d1$Es=="PRT_2015"]=6200006
d1$CSESid.4[d1$Es=="PRT_2015"]=6200004
d1$CSESid.5[d1$Es=="PRT_2015"]=6200020

d1$CSESid.1[d1$Es=="SRB_2012"]=6880001
d1$CSESid.2[d1$Es=="SRB_2012"]=6880002
d1$CSESid.3[d1$Es=="SRB_2012"]=6880004
d1$CSESid.4[d1$Es=="SRB_2012"]=6880005
d1$CSESid.5[d1$Es=="SRB_2012"]=6880006
d1$CSESid.6[d1$Es=="SRB_2012"]=6880007

d1$CSESid.1[d1$Es=="SVK_2010"]=7030001
d1$CSESid.2[d1$Es=="SVK_2010"]=7030003
d1$CSESid.3[d1$Es=="SVK_2010"]=7030002
d1$CSESid.4[d1$Es=="SVK_2010"]=7030004
d1$CSESid.5[d1$Es=="SVK_2010"]=7030008
d1$CSESid.6[d1$Es=="SVK_2010"]=7030006

d1$CSESid.1[d1$Es=="SVK_2016"]=7030001
d1$CSESid.2[d1$Es=="SVK_2016"]=7030002
d1$CSESid.3[d1$Es=="SVK_2016"]=7030009
d1$CSESid.4[d1$Es=="SVK_2016"]=7030006
d1$CSESid.5[d1$Es=="SVK_2016"]=7030012
d1$CSESid.6[d1$Es=="SVK_2016"]=7030014

d1$CSESid.1[d1$Es=="SVN_1996"]=7050004#F not coded, substituted with G
d1$CSESid.2[d1$Es=="SVN_1996"]=7050006
d1$CSESid.3[d1$Es=="SVN_1996"]=7050001
d1$CSESid.4[d1$Es=="SVN_1996"]=7050017
d1$CSESid.5[d1$Es=="SVN_1996"]=7050002
d1$CSESid.6[d1$Es=="SVN_1996"]=7050008

d1$CSESid.1[d1$Es=="SVN_2008"]=7050002
d1$CSESid.2[d1$Es=="SVN_2008"]=7050001
d1$CSESid.3[d1$Es=="SVN_2008"]=7050020
d1$CSESid.4[d1$Es=="SVN_2008"]=7050003
d1$CSESid.5[d1$Es=="SVN_2008"]=7050008
d1$CSESid.6[d1$Es=="SVN_2008"]=7050019

d1$CSESid.1[d1$Es=="SVN_2011"]=7050016
d1$CSESid.2[d1$Es=="SVN_2011"]=7050001
d1$CSESid.3[d1$Es=="SVN_2011"]=7050002
d1$CSESid.4[d1$Es=="SVN_2011"]=7050009
d1$CSESid.5[d1$Es=="SVN_2011"]=7050003
d1$CSESid.6[d1$Es=="SVN_2011"]=7050006

d1$CSESid.1[d1$Es=="SWE_1998"]=7520001
d1$CSESid.2[d1$Es=="SWE_1998"]=7520002
d1$CSESid.3[d1$Es=="SWE_1998"]=7520007
d1$CSESid.4[d1$Es=="SWE_1998"]=7520004
d1$CSESid.5[d1$Es=="SWE_1998"]=7520006
d1$CSESid.6[d1$Es=="SWE_1998"]=7520003

d1$CSESid.1[d1$Es=="SWE_2006"]=7520001
d1$CSESid.2[d1$Es=="SWE_2006"]=7520002
d1$CSESid.3[d1$Es=="SWE_2006"]=7520006
d1$CSESid.4[d1$Es=="SWE_2006"]=7520003
d1$CSESid.5[d1$Es=="SWE_2006"]=7520004
d1$CSESid.6[d1$Es=="SWE_2006"]=7520007

d1$CSESid.1[d1$Es=="SWE_2014"]=7520001
d1$CSESid.2[d1$Es=="SWE_2014"]=7520002
d1$CSESid.3[d1$Es=="SWE_2014"]=7520008
d1$CSESid.4[d1$Es=="SWE_2014"]=7520005
d1$CSESid.5[d1$Es=="SWE_2014"]=7520006
d1$CSESid.6[d1$Es=="SWE_2014"]=7520007

d1$CSESid.1[d1$Es=="THA_2007"]=7640014
d1$CSESid.2[d1$Es=="THA_2007"]=7640002
d1$CSESid.3[d1$Es=="THA_2007"]=7640021
d1$CSESid.4[d1$Es=="THA_2007"]=7640008
d1$CSESid.5[d1$Es=="THA_2007"]=7640060
d1$CSESid.6[d1$Es=="THA_2007"]=7640024

d1$CSESid.1[d1$Es=="THA_2011"]=7640001
d1$CSESid.2[d1$Es=="THA_2011"]=7640002
d1$CSESid.3[d1$Es=="THA_2011"]=7640003
d1$CSESid.4[d1$Es=="THA_2011"]=7640004
d1$CSESid.5[d1$Es=="THA_2011"]=7640005
d1$CSESid.6[d1$Es=="THA_2011"]=7640006

d1$CSESid.1[d1$Es=="TWN_1996"]=1580002
d1$CSESid.2[d1$Es=="TWN_1996"]=1580001
d1$CSESid.3[d1$Es=="TWN_1996"]=1580004

d1$CSESid.1[d1$Es=="ZAF_2009"]=7100001
d1$CSESid.2[d1$Es=="ZAF_2009"]=7100002
d1$CSESid.3[d1$Es=="ZAF_2009"]=7100008
d1$CSESid.4[d1$Es=="ZAF_2009"]=7100004
d1$CSESid.5[d1$Es=="ZAF_2009"]=7100022
d1$CSESid.6[d1$Es=="ZAF_2009"]=7100006

d1$CSESid.1[d1$Es=="ZAF_2014"]=7100001
d1$CSESid.2[d1$Es=="ZAF_2014"]=7100002
d1$CSESid.3[d1$Es=="ZAF_2014"]=7100003
d1$CSESid.4[d1$Es=="ZAF_2014"]=7100004
d1$CSESid.5[d1$Es=="ZAF_2014"]=7100005
d1$CSESid.6[d1$Es=="ZAF_2014"]=7100006


#d1 party identification (Pi):----

d1$Pi=NA

#assign Pi:
d1$Pi[d1$Es=="ARG_2015"&d1$IMD3005_3==0320001]=1#
d1$Pi[d1$Es=="ARG_2015"&d1$IMD3005_3==0320002]=2
d1$Pi[d1$Es=="ARG_2015"&d1$IMD3005_3==0320003]=3
d1$Pi[d1$Es=="ARG_2015"&d1$IMD3005_3==0320004]=4
d1$Pi[d1$Es=="ARG_2015"&d1$IMD3005_3==0320005]=5
d1$Pi[d1$Es=="ARG_2015"&d1$IMD3005_3==0320006]=6

d1$Pi[d1$Es=="AUS_1996"&d1$IMD3005_3==0360003]=1#
d1$Pi[d1$Es=="AUS_1996"&d1$IMD3005_3==0360001]=2
d1$Pi[d1$Es=="AUS_1996"&d1$IMD3005_3==0360002]=3
d1$Pi[d1$Es=="AUS_1996"&d1$IMD3005_3==0360004]=4

d1$Pi[d1$Es=="AUS_2004"&d1$IMD3005_3==0360001]=1#scaled up since E not coded
d1$Pi[d1$Es=="AUS_2004"&d1$IMD3005_3==0360003]=2
d1$Pi[d1$Es=="AUS_2004"&d1$IMD3005_3==0360005]=3
d1$Pi[d1$Es=="AUS_2004"&d1$IMD3005_3==0360002]=4
d1$Pi[d1$Es=="AUS_2004"&d1$IMD3005_3==0360004]=5
d1$Pi[d1$Es=="AUS_2004"&d1$IMD3005_3==0360008]=6

d1$Pi[d1$Es=="AUS_2007"&d1$IMD3005_3==0360003]=1#
d1$Pi[d1$Es=="AUS_2007"&d1$IMD3005_3==0360001]=2
d1$Pi[d1$Es=="AUS_2007"&d1$IMD3005_3==0360002]=3
d1$Pi[d1$Es=="AUS_2007"&d1$IMD3005_3==0360005]=4

d1$Pi[d1$Es=="AUS_2013"&d1$IMD3005_3==0360001]=1#scaled up E since D not coded
d1$Pi[d1$Es=="AUS_2013"&d1$IMD3005_3==0360003]=2
d1$Pi[d1$Es=="AUS_2013"&d1$IMD3005_3==0360005]=3
d1$Pi[d1$Es=="AUS_2013"&d1$IMD3005_3==0360002]=4

d1$Pi[d1$Es=="AUT_2013"&d1$IMD3005_3==0400001]=1#
d1$Pi[d1$Es=="AUT_2013"&d1$IMD3005_3==0400002]=2
d1$Pi[d1$Es=="AUT_2013"&d1$IMD3005_3==0400003]=3
d1$Pi[d1$Es=="AUT_2013"&d1$IMD3005_3==0400005]=4
d1$Pi[d1$Es=="AUT_2013"&d1$IMD3005_3==0400004]=5
d1$Pi[d1$Es=="AUT_2013"&d1$IMD3005_3==0400010]=6

d1$Pi[d1$Es=="BGR_2014"&d1$IMD3005_3==1000001]=1#
d1$Pi[d1$Es=="BGR_2014"&d1$IMD3005_3==1000020]=2
d1$Pi[d1$Es=="BGR_2014"&d1$IMD3005_3==1000003]=3
d1$Pi[d1$Es=="BGR_2014"&d1$IMD3005_3==1000004]=4
d1$Pi[d1$Es=="BGR_2014"&d1$IMD3005_3==1000005]=5
d1$Pi[d1$Es=="BGR_2014"&d1$IMD3005_3==1000006]=6

d1$Pi[d1$Es=="CAN_1997"&d1$IMD3005_3==1240001]=1#
d1$Pi[d1$Es=="CAN_1997"&d1$IMD3005_3==1240007]=2
d1$Pi[d1$Es=="CAN_1997"&d1$IMD3005_3==1240006]=3
d1$Pi[d1$Es=="CAN_1997"&d1$IMD3005_3==1240003]=4
d1$Pi[d1$Es=="CAN_1997"&d1$IMD3005_3==1240004]=5

d1$Pi[d1$Es=="CAN_2008"&d1$IMD3005_3==1240002]=1#
d1$Pi[d1$Es=="CAN_2008"&d1$IMD3005_3==1240001]=2
d1$Pi[d1$Es=="CAN_2008"&d1$IMD3005_3==1240004]=3
d1$Pi[d1$Es=="CAN_2008"&d1$IMD3005_3==1240003]=4
d1$Pi[d1$Es=="CAN_2008"&d1$IMD3005_3==1240005]=5

d1$Pi[d1$Es=="CAN_2011"&d1$IMD3005_3==1240002]=1#
d1$Pi[d1$Es=="CAN_2011"&d1$IMD3005_3==1240003]=2
d1$Pi[d1$Es=="CAN_2011"&d1$IMD3005_3==1240001]=3
d1$Pi[d1$Es=="CAN_2011"&d1$IMD3005_3==1240004]=4
d1$Pi[d1$Es=="CAN_2011"&d1$IMD3005_3==1240005]=5

d1$Pi[d1$Es=="CAN_2015"&d1$IMD3005_3==1240001]=1#
d1$Pi[d1$Es=="CAN_2015"&d1$IMD3005_3==1240002]=2
d1$Pi[d1$Es=="CAN_2015"&d1$IMD3005_3==1240003]=3
d1$Pi[d1$Es=="CAN_2015"&d1$IMD3005_3==1240004]=4
d1$Pi[d1$Es=="CAN_2015"&d1$IMD3005_3==1240005]=5

d1$Pi[d1$Es=="CZE_1996"&d1$IMD3005_3==2030002]=1#
d1$Pi[d1$Es=="CZE_1996"&d1$IMD3005_3==2030001]=2
d1$Pi[d1$Es=="CZE_1996"&d1$IMD3005_3==2030004]=3
d1$Pi[d1$Es=="CZE_1996"&d1$IMD3005_3==2030003]=4
d1$Pi[d1$Es=="CZE_1996"&d1$IMD3005_3==2030018]=5
d1$Pi[d1$Es=="CZE_1996"&d1$IMD3005_3==2030011]=6

d1$Pi[d1$Es=="CZE_2006"&d1$IMD3005_3==2030002]=1#
d1$Pi[d1$Es=="CZE_2006"&d1$IMD3005_3==2030001]=2
d1$Pi[d1$Es=="CZE_2006"&d1$IMD3005_3==2030004]=3
d1$Pi[d1$Es=="CZE_2006"&d1$IMD3005_3==2030003]=4
d1$Pi[d1$Es=="CZE_2006"&d1$IMD3005_3==2030005]=5
d1$Pi[d1$Es=="CZE_2006"&d1$IMD3005_3==2030074]=6

d1$Pi[d1$Es=="CZE_2010"&d1$IMD3005_3==2030001]=1#
d1$Pi[d1$Es=="CZE_2010"&d1$IMD3005_3==2030002]=2
d1$Pi[d1$Es=="CZE_2010"&d1$IMD3005_3==2030010]=3
d1$Pi[d1$Es=="CZE_2010"&d1$IMD3005_3==2030004]=4
d1$Pi[d1$Es=="CZE_2010"&d1$IMD3005_3==2030014]=5
d1$Pi[d1$Es=="CZE_2010"&d1$IMD3005_3==2030003]=6

d1$Pi[d1$Es=="CZE_2013"&d1$IMD3005_3==2030001]=1#
d1$Pi[d1$Es=="CZE_2013"&d1$IMD3005_3==2030006]=2
d1$Pi[d1$Es=="CZE_2013"&d1$IMD3005_3==2030004]=3
d1$Pi[d1$Es=="CZE_2013"&d1$IMD3005_3==2030010]=4
d1$Pi[d1$Es=="CZE_2013"&d1$IMD3005_3==2030002]=5
d1$Pi[d1$Es=="CZE_2013"&d1$IMD3005_3==2030013]=6

d1$Pi[d1$Es=="DEU_1998"&d1$IMD3005_3==2760004]=1#DEU:scale up letters after removing CSU
d1$Pi[d1$Es=="DEU_1998"&d1$IMD3005_3==2760001]=2##note: no need to change 2760001 since it is already vote for CDU, or CSU in Bavaria
d1$Pi[d1$Es=="DEU_1998"&d1$IMD3005_3==2760005]=3
d1$Pi[d1$Es=="DEU_1998"&d1$IMD3005_3==2760006]=4
d1$Pi[d1$Es=="DEU_1998"&d1$IMD3005_3==2760007]=5

d1$Pi[d1$Es=="DEU_2005"&d1$IMD3005_3==2760004]=1#(comes from module 3, not module 2)
d1$Pi[d1$Es=="DEU_2005"&d1$IMD3005_3==2760001]=2
d1$Pi[d1$Es=="DEU_2005"&d1$IMD3005_3==2760006]=3
d1$Pi[d1$Es=="DEU_2005"&d1$IMD3005_3==2760007]=4
d1$Pi[d1$Es=="DEU_2005"&d1$IMD3005_3==2760005]=5
d1$Pi[d1$Es=="DEU_2005"&d1$IMD3005_3==2760009]=6

d1$Pi[d1$Es=="DEU_2009"&d1$IMD3005_3==2760001]=1#
d1$Pi[d1$Es=="DEU_2009"&d1$IMD3005_3==2760004]=2
d1$Pi[d1$Es=="DEU_2009"&d1$IMD3005_3==2760006]=3
d1$Pi[d1$Es=="DEU_2009"&d1$IMD3005_3==2760007]=4
d1$Pi[d1$Es=="DEU_2009"&d1$IMD3005_3==2760005]=5

d1$Pi[d1$Es=="DEU_2013"&d1$IMD3005_3==2760001]=1#
d1$Pi[d1$Es=="DEU_2013"&d1$IMD3005_3==2760004]=2
d1$Pi[d1$Es=="DEU_2013"&d1$IMD3005_3==2760007]=3
d1$Pi[d1$Es=="DEU_2013"&d1$IMD3005_3==2760005]=4
d1$Pi[d1$Es=="DEU_2013"&d1$IMD3005_3==2760006]=5

d1$Pi[d1$Es=="DNK_1998"&d1$IMD3005_3==2080001]=1#
d1$Pi[d1$Es=="DNK_1998"&d1$IMD3005_3==2080002]=2
d1$Pi[d1$Es=="DNK_1998"&d1$IMD3005_3==2080007]=3
d1$Pi[d1$Es=="DNK_1998"&d1$IMD3005_3==2080004]=4
d1$Pi[d1$Es=="DNK_1998"&d1$IMD3005_3==2080010]=5
d1$Pi[d1$Es=="DNK_1998"&d1$IMD3005_3==2080011]=6

d1$Pi[d1$Es=="DNK_2007"&d1$IMD3005_3==2080002]=1#
d1$Pi[d1$Es=="DNK_2007"&d1$IMD3005_3==2080001]=2
d1$Pi[d1$Es=="DNK_2007"&d1$IMD3005_3==2080010]=3
d1$Pi[d1$Es=="DNK_2007"&d1$IMD3005_3==2080004]=4
d1$Pi[d1$Es=="DNK_2007"&d1$IMD3005_3==2080007]=5
d1$Pi[d1$Es=="DNK_2007"&d1$IMD3005_3==2080006]=6

d1$Pi[d1$Es=="ESP_1996"&d1$IMD3005_3==7240002]=1#
d1$Pi[d1$Es=="ESP_1996"&d1$IMD3005_3==7240001]=2
d1$Pi[d1$Es=="ESP_1996"&d1$IMD3005_3==7240004]=3
d1$Pi[d1$Es=="ESP_1996"&d1$IMD3005_3==7240003]=4
d1$Pi[d1$Es=="ESP_1996"&d1$IMD3005_3==7240005]=5

d1$Pi[d1$Es=="ESP_2000"&d1$IMD3005_3==7240002]=1#
d1$Pi[d1$Es=="ESP_2000"&d1$IMD3005_3==7240001]=2
d1$Pi[d1$Es=="ESP_2000"&d1$IMD3005_3==7240004]=3
d1$Pi[d1$Es=="ESP_2000"&d1$IMD3005_3==7240003]=4
d1$Pi[d1$Es=="ESP_2000"&d1$IMD3005_3==7240005]=5

d1$Pi[d1$Es=="ESP_2008"&d1$IMD3005_3==7240001]=1#
d1$Pi[d1$Es=="ESP_2008"&d1$IMD3005_3==7240002]=2
d1$Pi[d1$Es=="ESP_2008"&d1$IMD3005_3==7240003]=3
d1$Pi[d1$Es=="ESP_2008"&d1$IMD3005_3==7240005]=4
d1$Pi[d1$Es=="ESP_2008"&d1$IMD3005_3==7240009]=5
d1$Pi[d1$Es=="ESP_2008"&d1$IMD3005_3==7240004]=6

d1$Pi[d1$Es=="FIN_2007"&d1$IMD3005_3==2460001]=1#
d1$Pi[d1$Es=="FIN_2007"&d1$IMD3005_3==2460003]=2
d1$Pi[d1$Es=="FIN_2007"&d1$IMD3005_3==2460002]=3
d1$Pi[d1$Es=="FIN_2007"&d1$IMD3005_3==2460006]=4
d1$Pi[d1$Es=="FIN_2007"&d1$IMD3005_3==2460005]=5
d1$Pi[d1$Es=="FIN_2007"&d1$IMD3005_3==2460008]=6

d1$Pi[d1$Es=="FIN_2011"&d1$IMD3005_3==2460003]=1#
d1$Pi[d1$Es=="FIN_2011"&d1$IMD3005_3==2460002]=2
d1$Pi[d1$Es=="FIN_2011"&d1$IMD3005_3==2460004]=3
d1$Pi[d1$Es=="FIN_2011"&d1$IMD3005_3==2460001]=4
d1$Pi[d1$Es=="FIN_2011"&d1$IMD3005_3==2460006]=5
d1$Pi[d1$Es=="FIN_2011"&d1$IMD3005_3==2460005]=6

d1$Pi[d1$Es=="FIN_2015"&d1$IMD3005_3==2460001]=1#
d1$Pi[d1$Es=="FIN_2015"&d1$IMD3005_3==2460003]=2
d1$Pi[d1$Es=="FIN_2015"&d1$IMD3005_3==2460004]=3
d1$Pi[d1$Es=="FIN_2015"&d1$IMD3005_3==2460002]=4
d1$Pi[d1$Es=="FIN_2015"&d1$IMD3005_3==2460005]=5
d1$Pi[d1$Es=="FIN_2015"&d1$IMD3005_3==2460006]=6

d1$Pi[d1$Es=="FRA_2007"&d1$IMD3005_3==2500001]=1#
d1$Pi[d1$Es=="FRA_2007"&d1$IMD3005_3==2500002]=2
d1$Pi[d1$Es=="FRA_2007"&d1$IMD3005_3==2500003]=3
d1$Pi[d1$Es=="FRA_2007"&d1$IMD3005_3==2500007]=4
d1$Pi[d1$Es=="FRA_2007"&d1$IMD3005_3==2500004]=5
d1$Pi[d1$Es=="FRA_2007"&d1$IMD3005_3==2500005]=6

d1$Pi[d1$Es=="GBR_1997"&d1$IMD3005_3==8260002]=1#
d1$Pi[d1$Es=="GBR_1997"&d1$IMD3005_3==8260001]=2
d1$Pi[d1$Es=="GBR_1997"&d1$IMD3005_3==8260003]=3
d1$Pi[d1$Es=="GBR_1997"&d1$IMD3005_3==8260004]=4
d1$Pi[d1$Es=="GBR_1997"&d1$IMD3005_3==8260006]=5

d1$Pi[d1$Es=="GBR_2015"&d1$IMD3005_3==8260001]=1#
d1$Pi[d1$Es=="GBR_2015"&d1$IMD3005_3==8260002]=2
d1$Pi[d1$Es=="GBR_2015"&d1$IMD3005_3==8260007]=3
d1$Pi[d1$Es=="GBR_2015"&d1$IMD3005_3==8260003]=4
d1$Pi[d1$Es=="GBR_2015"&d1$IMD3005_3==8260004]=5
d1$Pi[d1$Es=="GBR_2015"&d1$IMD3005_3==8260005]=6

d1$Pi[d1$Es=="GRC_2009"&d1$IMD3005_3==3000001]=1#
d1$Pi[d1$Es=="GRC_2009"&d1$IMD3005_3==3000002]=2
d1$Pi[d1$Es=="GRC_2009"&d1$IMD3005_3==3000004]=3
d1$Pi[d1$Es=="GRC_2009"&d1$IMD3005_3==3000019]=4
d1$Pi[d1$Es=="GRC_2009"&d1$IMD3005_3==3000003]=5
d1$Pi[d1$Es=="GRC_2009"&d1$IMD3005_3==3000005]=6

d1$Pi[d1$Es=="GRC_2012"&d1$IMD3005_3==3000002]=1#
d1$Pi[d1$Es=="GRC_2012"&d1$IMD3005_3==3000003]=2
d1$Pi[d1$Es=="GRC_2012"&d1$IMD3005_3==3000001]=3
d1$Pi[d1$Es=="GRC_2012"&d1$IMD3005_3==3000007]=4
d1$Pi[d1$Es=="GRC_2012"&d1$IMD3005_3==3000006]=5
d1$Pi[d1$Es=="GRC_2012"&d1$IMD3005_3==3000012]=6

d1$Pi[d1$Es=="GRC_2015"&d1$IMD3005_3==3000003]=1#
d1$Pi[d1$Es=="GRC_2015"&d1$IMD3005_3==3000002]=2
d1$Pi[d1$Es=="GRC_2015"&d1$IMD3005_3==3000006]=3
d1$Pi[d1$Es=="GRC_2015"&d1$IMD3005_3==3000013]=4
d1$Pi[d1$Es=="GRC_2015"&d1$IMD3005_3==3000004]=5
d1$Pi[d1$Es=="GRC_2015"&d1$IMD3005_3==3000007]=6

d1$Pi[d1$Es=="HRV_2007"&d1$IMD3005_3==1910001]=1#
d1$Pi[d1$Es=="HRV_2007"&d1$IMD3005_3==1910002]=2
d1$Pi[d1$Es=="HRV_2007"&d1$IMD3005_3==1910003]=3
d1$Pi[d1$Es=="HRV_2007"&d1$IMD3005_3==1910007]=4
d1$Pi[d1$Es=="HRV_2007"&d1$IMD3005_3==1910006]=5
d1$Pi[d1$Es=="HRV_2007"&d1$IMD3005_3==1910010]=6

d1$Pi[d1$Es=="IRL_2007"&d1$IMD3005_3==3720001]=1#
d1$Pi[d1$Es=="IRL_2007"&d1$IMD3005_3==3720002]=2
d1$Pi[d1$Es=="IRL_2007"&d1$IMD3005_3==3720003]=3
d1$Pi[d1$Es=="IRL_2007"&d1$IMD3005_3==3720005]=4
d1$Pi[d1$Es=="IRL_2007"&d1$IMD3005_3==3720004]=5
d1$Pi[d1$Es=="IRL_2007"&d1$IMD3005_3==3720006]=6

d1$Pi[d1$Es=="IRL_2011"&d1$IMD3005_3==3720002]=1#E not coded, substituted with F
d1$Pi[d1$Es=="IRL_2011"&d1$IMD3005_3==3720003]=2
d1$Pi[d1$Es=="IRL_2011"&d1$IMD3005_3==3720001]=3
d1$Pi[d1$Es=="IRL_2011"&d1$IMD3005_3==3720004]=4
d1$Pi[d1$Es=="IRL_2011"&d1$IMD3005_3==3720005]=5

d1$Pi[d1$Es=="ISL_1999"&d1$IMD3005_3==3520001]=1#
d1$Pi[d1$Es=="ISL_1999"&d1$IMD3005_3==3520002]=2
d1$Pi[d1$Es=="ISL_1999"&d1$IMD3005_3==3520003]=3
d1$Pi[d1$Es=="ISL_1999"&d1$IMD3005_3==3520005]=4
d1$Pi[d1$Es=="ISL_1999"&d1$IMD3005_3==3520004]=5

d1$Pi[d1$Es=="ISL_2007"&d1$IMD3005_3==3520001]=1#
d1$Pi[d1$Es=="ISL_2007"&d1$IMD3005_3==3520002]=2
d1$Pi[d1$Es=="ISL_2007"&d1$IMD3005_3==3520005]=3
d1$Pi[d1$Es=="ISL_2007"&d1$IMD3005_3==3520003]=4
d1$Pi[d1$Es=="ISL_2007"&d1$IMD3005_3==3520004]=5
d1$Pi[d1$Es=="ISL_2007"&d1$IMD3005_3==3520013]=6

d1$Pi[d1$Es=="ISL_2009"&d1$IMD3005_3==3520002]=1#
d1$Pi[d1$Es=="ISL_2009"&d1$IMD3005_3==3520001]=2
d1$Pi[d1$Es=="ISL_2009"&d1$IMD3005_3==3520005]=3
d1$Pi[d1$Es=="ISL_2009"&d1$IMD3005_3==3520003]=4
d1$Pi[d1$Es=="ISL_2009"&d1$IMD3005_3==3520009]=5
d1$Pi[d1$Es=="ISL_2009"&d1$IMD3005_3==3520004]=6

d1$Pi[d1$Es=="ISL_2013"&d1$IMD3005_3==3520001]=1#
d1$Pi[d1$Es=="ISL_2013"&d1$IMD3005_3==3520003]=2
d1$Pi[d1$Es=="ISL_2013"&d1$IMD3005_3==3520002]=3
d1$Pi[d1$Es=="ISL_2013"&d1$IMD3005_3==3520005]=4
d1$Pi[d1$Es=="ISL_2013"&d1$IMD3005_3==3520007]=5
d1$Pi[d1$Es=="ISL_2013"&d1$IMD3005_3==3520008]=6

d1$Pi[d1$Es=="ISR_1996"&d1$IMD3005_3==3760002]=1#
d1$Pi[d1$Es=="ISR_1996"&d1$IMD3005_3==3760001]=2
d1$Pi[d1$Es=="ISR_1996"&d1$IMD3005_3==3760003]=3
d1$Pi[d1$Es=="ISR_1996"&d1$IMD3005_3==3760014]=4
d1$Pi[d1$Es=="ISR_1996"&d1$IMD3005_3==3760004]=5

d1$Pi[d1$Es=="ISR_2006"&d1$IMD3005_3==3760016]=1#
d1$Pi[d1$Es=="ISR_2006"&d1$IMD3005_3==3760002]=2
d1$Pi[d1$Es=="ISR_2006"&d1$IMD3005_3==3760003]=3
d1$Pi[d1$Es=="ISR_2006"&d1$IMD3005_3==3760001]=4
d1$Pi[d1$Es=="ISR_2006"&d1$IMD3005_3==3760021]=5
d1$Pi[d1$Es=="ISR_2006"&d1$IMD3005_3==3760050]=6

d1$Pi[d1$Es=="ISR_2013"&d1$IMD3005_3==3760011]=1#F not coded, substituted with G
d1$Pi[d1$Es=="ISR_2013"&d1$IMD3005_3==3760007]=2
d1$Pi[d1$Es=="ISR_2013"&d1$IMD3005_3==3760002]=3
d1$Pi[d1$Es=="ISR_2013"&d1$IMD3005_3==3760008]=4
d1$Pi[d1$Es=="ISR_2013"&d1$IMD3005_3==3760003]=5
d1$Pi[d1$Es=="ISR_2013"&d1$IMD3005_3==3760009]=6

d1$Pi[d1$Es=="LVA_2010"&d1$IMD3005_3==4280002]=1#
d1$Pi[d1$Es=="LVA_2010"&d1$IMD3005_3==4280001]=2
d1$Pi[d1$Es=="LVA_2010"&d1$IMD3005_3==4280005]=3
d1$Pi[d1$Es=="LVA_2010"&d1$IMD3005_3==4280003]=4
d1$Pi[d1$Es=="LVA_2010"&d1$IMD3005_3==4280004]=5
d1$Pi[d1$Es=="LVA_2010"&d1$IMD3005_3==4280006]=6

d1$Pi[d1$Es=="LVA_2011"&d1$IMD3005_3==4280001]=1#
d1$Pi[d1$Es=="LVA_2011"&d1$IMD3005_3==4280007]=2
d1$Pi[d1$Es=="LVA_2011"&d1$IMD3005_3==4280002]=3
d1$Pi[d1$Es=="LVA_2011"&d1$IMD3005_3==4280003]=4
d1$Pi[d1$Es=="LVA_2011"&d1$IMD3005_3==4280005]=5
d1$Pi[d1$Es=="LVA_2011"&d1$IMD3005_3==4280036]=6

d1$Pi[d1$Es=="LVA_2014"&d1$IMD3005_3==4280001]=1#
d1$Pi[d1$Es=="LVA_2014"&d1$IMD3005_3==4280002]=2
d1$Pi[d1$Es=="LVA_2014"&d1$IMD3005_3==4280005]=3
d1$Pi[d1$Es=="LVA_2014"&d1$IMD3005_3==4280003]=4
d1$Pi[d1$Es=="LVA_2014"&d1$IMD3005_3==4280013]=5
d1$Pi[d1$Es=="LVA_2014"&d1$IMD3005_3==4280014]=6

d1$Pi[d1$Es=="MEX_2000"&d1$IMD3005_3==4840022]=1#
d1$Pi[d1$Es=="MEX_2000"&d1$IMD3005_3==4840001]=2
d1$Pi[d1$Es=="MEX_2000"&d1$IMD3005_3==4840023]=3

d1$Pi[d1$Es=="MEX_2006"&d1$IMD3005_3==4840002]=1#
d1$Pi[d1$Es=="MEX_2006"&d1$IMD3005_3==4840003]=2
d1$Pi[d1$Es=="MEX_2006"&d1$IMD3005_3==4840001]=3

d1$Pi[d1$Es=="MEX_2009"&d1$IMD3005_3==4840001]=1#
d1$Pi[d1$Es=="MEX_2009"&d1$IMD3005_3==4840002]=2
d1$Pi[d1$Es=="MEX_2009"&d1$IMD3005_3==4840003]=3

d1$Pi[d1$Es=="MNE_2012"&d1$IMD3005_3==4990011]=1#
d1$Pi[d1$Es=="MNE_2012"&d1$IMD3005_3==4990012]=2
d1$Pi[d1$Es=="MNE_2012"&d1$IMD3005_3==4990003]=3
d1$Pi[d1$Es=="MNE_2012"&d1$IMD3005_3==4990006]=4
d1$Pi[d1$Es=="MNE_2012"&d1$IMD3005_3==4990009]=5
d1$Pi[d1$Es=="MNE_2012"&d1$IMD3005_3==4990037]=6

d1$Pi[d1$Es=="NLD_1998"&d1$IMD3005_3==5280002]=1#
d1$Pi[d1$Es=="NLD_1998"&d1$IMD3005_3==5280003]=2
d1$Pi[d1$Es=="NLD_1998"&d1$IMD3005_3==5280001]=3
d1$Pi[d1$Es=="NLD_1998"&d1$IMD3005_3==5280004]=4
d1$Pi[d1$Es=="NLD_1998"&d1$IMD3005_3==5280005]=5
d1$Pi[d1$Es=="NLD_1998"&d1$IMD3005_3==5280006]=6

d1$Pi[d1$Es=="NLD_2006"&d1$IMD3005_3==5280001]=1#
d1$Pi[d1$Es=="NLD_2006"&d1$IMD3005_3==5280002]=2
d1$Pi[d1$Es=="NLD_2006"&d1$IMD3005_3==5280006]=3
d1$Pi[d1$Es=="NLD_2006"&d1$IMD3005_3==5280003]=4
d1$Pi[d1$Es=="NLD_2006"&d1$IMD3005_3==5280009]=5
d1$Pi[d1$Es=="NLD_2006"&d1$IMD3005_3==5280005]=6

d1$Pi[d1$Es=="NLD_2010"&d1$IMD3005_3==5280003]=1#
d1$Pi[d1$Es=="NLD_2010"&d1$IMD3005_3==5280002]=2
d1$Pi[d1$Es=="NLD_2010"&d1$IMD3005_3==5280009]=3
d1$Pi[d1$Es=="NLD_2010"&d1$IMD3005_3==5280001]=4
d1$Pi[d1$Es=="NLD_2010"&d1$IMD3005_3==5280006]=5
d1$Pi[d1$Es=="NLD_2010"&d1$IMD3005_3==5280004]=6

d1$Pi[d1$Es=="NOR_1997"&d1$IMD3005_3==5780001]=1#
d1$Pi[d1$Es=="NOR_1997"&d1$IMD3005_3==5780003]=2
d1$Pi[d1$Es=="NOR_1997"&d1$IMD3005_3==5780002]=3
d1$Pi[d1$Es=="NOR_1997"&d1$IMD3005_3==5780007]=4
d1$Pi[d1$Es=="NOR_1997"&d1$IMD3005_3==5780006]=5
d1$Pi[d1$Es=="NOR_1997"&d1$IMD3005_3==5780004]=6

d1$Pi[d1$Es=="NOR_2005"&d1$IMD3005_3==5780001]=1#
d1$Pi[d1$Es=="NOR_2005"&d1$IMD3005_3==5780003]=2
d1$Pi[d1$Es=="NOR_2005"&d1$IMD3005_3==5780002]=3
d1$Pi[d1$Es=="NOR_2005"&d1$IMD3005_3==5780004]=4
d1$Pi[d1$Es=="NOR_2005"&d1$IMD3005_3==5780007]=5
d1$Pi[d1$Es=="NOR_2005"&d1$IMD3005_3==5780006]=6

d1$Pi[d1$Es=="NOR_2009"&d1$IMD3005_3==5780001]=1#
d1$Pi[d1$Es=="NOR_2009"&d1$IMD3005_3==5780003]=2
d1$Pi[d1$Es=="NOR_2009"&d1$IMD3005_3==5780002]=3
d1$Pi[d1$Es=="NOR_2009"&d1$IMD3005_3==5780004]=4
d1$Pi[d1$Es=="NOR_2009"&d1$IMD3005_3==5780006]=5
d1$Pi[d1$Es=="NOR_2009"&d1$IMD3005_3==5780007]=6

d1$Pi[d1$Es=="NOR_2013"&d1$IMD3005_3==5780001]=1#
d1$Pi[d1$Es=="NOR_2013"&d1$IMD3005_3==5780002]=2
d1$Pi[d1$Es=="NOR_2013"&d1$IMD3005_3==5780003]=3
d1$Pi[d1$Es=="NOR_2013"&d1$IMD3005_3==5780007]=4
d1$Pi[d1$Es=="NOR_2013"&d1$IMD3005_3==5780006]=5
d1$Pi[d1$Es=="NOR_2013"&d1$IMD3005_3==5780008]=6

d1$Pi[d1$Es=="NZL_1996"&d1$IMD3005_3==5540001]=1#
d1$Pi[d1$Es=="NZL_1996"&d1$IMD3005_3==5540002]=2
d1$Pi[d1$Es=="NZL_1996"&d1$IMD3005_3==5540003]=3
d1$Pi[d1$Es=="NZL_1996"&d1$IMD3005_3==5540009]=4
d1$Pi[d1$Es=="NZL_1996"&d1$IMD3005_3==5540004]=5
d1$Pi[d1$Es=="NZL_1996"&d1$IMD3005_3==5540037]=6

d1$Pi[d1$Es=="NZL_2008"&d1$IMD3005_3==5540001]=1#
d1$Pi[d1$Es=="NZL_2008"&d1$IMD3005_3==5540002]=2
d1$Pi[d1$Es=="NZL_2008"&d1$IMD3005_3==5540005]=3
d1$Pi[d1$Es=="NZL_2008"&d1$IMD3005_3==5540003]=4
d1$Pi[d1$Es=="NZL_2008"&d1$IMD3005_3==5540004]=5
d1$Pi[d1$Es=="NZL_2008"&d1$IMD3005_3==5540008]=6

d1$Pi[d1$Es=="NZL_2011"&d1$IMD3005_3==5540001]=1#
d1$Pi[d1$Es=="NZL_2011"&d1$IMD3005_3==5540002]=2
d1$Pi[d1$Es=="NZL_2011"&d1$IMD3005_3==5540005]=3
d1$Pi[d1$Es=="NZL_2011"&d1$IMD3005_3==5540003]=4
d1$Pi[d1$Es=="NZL_2011"&d1$IMD3005_3==5540007]=5
d1$Pi[d1$Es=="NZL_2011"&d1$IMD3005_3==5540008]=6

d1$Pi[d1$Es=="NZL_2014"&d1$IMD3005_3==5540001]=1#
d1$Pi[d1$Es=="NZL_2014"&d1$IMD3005_3==5540002]=2
d1$Pi[d1$Es=="NZL_2014"&d1$IMD3005_3==5540005]=3
d1$Pi[d1$Es=="NZL_2014"&d1$IMD3005_3==5540003]=4
d1$Pi[d1$Es=="NZL_2014"&d1$IMD3005_3==5540007]=5
d1$Pi[d1$Es=="NZL_2014"&d1$IMD3005_3==5540010]=6

d1$Pi[d1$Es=="PER_2001"&d1$IMD3005_3==6040008]=1#
d1$Pi[d1$Es=="PER_2001"&d1$IMD3005_3==6040026]=2
d1$Pi[d1$Es=="PER_2001"&d1$IMD3005_3==6040052]=3
d1$Pi[d1$Es=="PER_2001"&d1$IMD3005_3==6040023]=4
d1$Pi[d1$Es=="PER_2001"&d1$IMD3005_3==6040030]=5
d1$Pi[d1$Es=="PER_2001"&d1$IMD3005_3==6040027]=6

d1$Pi[d1$Es=="PER_2011"&d1$IMD3005_3==6040050]=1#
d1$Pi[d1$Es=="PER_2011"&d1$IMD3005_3==6040001]=2
d1$Pi[d1$Es=="PER_2011"&d1$IMD3005_3==6040008]=3
d1$Pi[d1$Es=="PER_2011"&d1$IMD3005_3==6040051]=4
d1$Pi[d1$Es=="PER_2011"&d1$IMD3005_3==6040015]=5

d1$Pi[d1$Es=="PER_2016"&d1$IMD3005_3==6040001]=1#
d1$Pi[d1$Es=="PER_2016"&d1$IMD3005_3==6040002]=2
d1$Pi[d1$Es=="PER_2016"&d1$IMD3005_3==6040003]=3
d1$Pi[d1$Es=="PER_2016"&d1$IMD3005_3==6040004]=4
d1$Pi[d1$Es=="PER_2016"&d1$IMD3005_3==6040005]=5
d1$Pi[d1$Es=="PER_2016"&d1$IMD3005_3==6040006]=6

d1$Pi[d1$Es=="POL_1997"&d1$IMD3005_3==6160004]=1#
d1$Pi[d1$Es=="POL_1997"&d1$IMD3005_3==6160009]=2
d1$Pi[d1$Es=="POL_1997"&d1$IMD3005_3==6160008]=3
d1$Pi[d1$Es=="POL_1997"&d1$IMD3005_3==6160003]=4
d1$Pi[d1$Es=="POL_1997"&d1$IMD3005_3==6160010]=5
d1$Pi[d1$Es=="POL_1997"&d1$IMD3005_3==6160007]=6

d1$Pi[d1$Es=="POL_2005"&d1$IMD3005_3==6160002]=1#F not coded, substituted with H
d1$Pi[d1$Es=="POL_2005"&d1$IMD3005_3==6160001]=2
d1$Pi[d1$Es=="POL_2005"&d1$IMD3005_3==6160024]=3
d1$Pi[d1$Es=="POL_2005"&d1$IMD3005_3==6160009]=4
d1$Pi[d1$Es=="POL_2005"&d1$IMD3005_3==6160014]=5
d1$Pi[d1$Es=="POL_2005"&d1$IMD3005_3==6160068]=6

d1$Pi[d1$Es=="POL_2007"&d1$IMD3005_3==6160001]=1#
d1$Pi[d1$Es=="POL_2007"&d1$IMD3005_3==6160002]=2
d1$Pi[d1$Es=="POL_2007"&d1$IMD3005_3==6160020]=3
d1$Pi[d1$Es=="POL_2007"&d1$IMD3005_3==6160003]=4

d1$Pi[d1$Es=="POL_2011"&d1$IMD3005_3==6160001]=1#
d1$Pi[d1$Es=="POL_2011"&d1$IMD3005_3==6160002]=2
d1$Pi[d1$Es=="POL_2011"&d1$IMD3005_3==6160017]=3
d1$Pi[d1$Es=="POL_2011"&d1$IMD3005_3==6160003]=4
d1$Pi[d1$Es=="POL_2011"&d1$IMD3005_3==6160009]=5

d1$Pi[d1$Es=="PRT_2002"&d1$IMD3005_3==6200001]=1#
d1$Pi[d1$Es=="PRT_2002"&d1$IMD3005_3==6200002]=2
d1$Pi[d1$Es=="PRT_2002"&d1$IMD3005_3==6200003]=3
d1$Pi[d1$Es=="PRT_2002"&d1$IMD3005_3==6200004]=4
d1$Pi[d1$Es=="PRT_2002"&d1$IMD3005_3==6200006]=5
d1$Pi[d1$Es=="PRT_2002"&d1$IMD3005_3==6200007]=6

d1$Pi[d1$Es=="PRT_2009"&d1$IMD3005_3==6200002]=1#
d1$Pi[d1$Es=="PRT_2009"&d1$IMD3005_3==6200001]=2
d1$Pi[d1$Es=="PRT_2009"&d1$IMD3005_3==6200003]=3
d1$Pi[d1$Es=="PRT_2009"&d1$IMD3005_3==6200006]=4
d1$Pi[d1$Es=="PRT_2009"&d1$IMD3005_3==6200004]=5

d1$Pi[d1$Es=="PRT_2015"&d1$IMD3005_3==6200011]=1#
d1$Pi[d1$Es=="PRT_2015"&d1$IMD3005_3==6200002]=2
d1$Pi[d1$Es=="PRT_2015"&d1$IMD3005_3==6200006]=3
d1$Pi[d1$Es=="PRT_2015"&d1$IMD3005_3==6200004]=4
d1$Pi[d1$Es=="PRT_2015"&d1$IMD3005_3==6200020]=5

d1$Pi[d1$Es=="SRB_2012"&d1$IMD3005_3==6880001]=1#
d1$Pi[d1$Es=="SRB_2012"&d1$IMD3005_3==6880002]=2
d1$Pi[d1$Es=="SRB_2012"&d1$IMD3005_3==6880004]=3
d1$Pi[d1$Es=="SRB_2012"&d1$IMD3005_3==6880005]=4
d1$Pi[d1$Es=="SRB_2012"&d1$IMD3005_3==6880006]=5
d1$Pi[d1$Es=="SRB_2012"&d1$IMD3005_3==6880007]=6

d1$Pi[d1$Es=="SVK_2010"&d1$IMD3005_3==7030001]=1#
d1$Pi[d1$Es=="SVK_2010"&d1$IMD3005_3==7030003]=2
d1$Pi[d1$Es=="SVK_2010"&d1$IMD3005_3==7030002]=3
d1$Pi[d1$Es=="SVK_2010"&d1$IMD3005_3==7030004]=4
d1$Pi[d1$Es=="SVK_2010"&d1$IMD3005_3==7030008]=5
d1$Pi[d1$Es=="SVK_2010"&d1$IMD3005_3==7030006]=6

d1$Pi[d1$Es=="SVK_2016"&d1$IMD3005_3==7030001]=1#
d1$Pi[d1$Es=="SVK_2016"&d1$IMD3005_3==7030002]=2
d1$Pi[d1$Es=="SVK_2016"&d1$IMD3005_3==7030009]=3
d1$Pi[d1$Es=="SVK_2016"&d1$IMD3005_3==7030006]=4
d1$Pi[d1$Es=="SVK_2016"&d1$IMD3005_3==7030012]=5
d1$Pi[d1$Es=="SVK_2016"&d1$IMD3005_3==7030014]=6

d1$Pi[d1$Es=="SVN_1996"&d1$IMD3005_3==7050004]=1#F not coded, substituted with G
d1$Pi[d1$Es=="SVN_1996"&d1$IMD3005_3==7050006]=2
d1$Pi[d1$Es=="SVN_1996"&d1$IMD3005_3==7050001]=3
d1$Pi[d1$Es=="SVN_1996"&d1$IMD3005_3==7050017]=4
d1$Pi[d1$Es=="SVN_1996"&d1$IMD3005_3==7050002]=5
d1$Pi[d1$Es=="SVN_1996"&d1$IMD3005_3==7050008]=6

d1$Pi[d1$Es=="SVN_2008"&d1$IMD3005_3==7050002]=1#
d1$Pi[d1$Es=="SVN_2008"&d1$IMD3005_3==7050001]=2
d1$Pi[d1$Es=="SVN_2008"&d1$IMD3005_3==7050020]=3
d1$Pi[d1$Es=="SVN_2008"&d1$IMD3005_3==7050003]=4
d1$Pi[d1$Es=="SVN_2008"&d1$IMD3005_3==7050008]=5
d1$Pi[d1$Es=="SVN_2008"&d1$IMD3005_3==7050019]=6

d1$Pi[d1$Es=="SVN_2011"&d1$IMD3005_3==7050016]=1#
d1$Pi[d1$Es=="SVN_2011"&d1$IMD3005_3==7050001]=2
d1$Pi[d1$Es=="SVN_2011"&d1$IMD3005_3==7050002]=3
d1$Pi[d1$Es=="SVN_2011"&d1$IMD3005_3==7050009]=4
d1$Pi[d1$Es=="SVN_2011"&d1$IMD3005_3==7050003]=5
d1$Pi[d1$Es=="SVN_2011"&d1$IMD3005_3==7050006]=6

d1$Pi[d1$Es=="SWE_1998"&d1$IMD3005_3==7520001]=1#
d1$Pi[d1$Es=="SWE_1998"&d1$IMD3005_3==7520002]=2
d1$Pi[d1$Es=="SWE_1998"&d1$IMD3005_3==7520007]=3
d1$Pi[d1$Es=="SWE_1998"&d1$IMD3005_3==7520004]=4
d1$Pi[d1$Es=="SWE_1998"&d1$IMD3005_3==7520006]=5
d1$Pi[d1$Es=="SWE_1998"&d1$IMD3005_3==7520003]=6

d1$Pi[d1$Es=="SWE_2006"&d1$IMD3005_3==7520001]=1#
d1$Pi[d1$Es=="SWE_2006"&d1$IMD3005_3==7520002]=2
d1$Pi[d1$Es=="SWE_2006"&d1$IMD3005_3==7520006]=3
d1$Pi[d1$Es=="SWE_2006"&d1$IMD3005_3==7520003]=4
d1$Pi[d1$Es=="SWE_2006"&d1$IMD3005_3==7520004]=5
d1$Pi[d1$Es=="SWE_2006"&d1$IMD3005_3==7520007]=6

d1$Pi[d1$Es=="SWE_2014"&d1$IMD3005_3==7520001]=1#
d1$Pi[d1$Es=="SWE_2014"&d1$IMD3005_3==7520002]=2
d1$Pi[d1$Es=="SWE_2014"&d1$IMD3005_3==7520008]=3
d1$Pi[d1$Es=="SWE_2014"&d1$IMD3005_3==7520005]=4
d1$Pi[d1$Es=="SWE_2014"&d1$IMD3005_3==7520006]=5
d1$Pi[d1$Es=="SWE_2014"&d1$IMD3005_3==7520007]=6

d1$Pi[d1$Es=="THA_2007"&d1$IMD3005_3==7640014]=1
d1$Pi[d1$Es=="THA_2007"&d1$IMD3005_3==7640002]=2
d1$Pi[d1$Es=="THA_2007"&d1$IMD3005_3==7640021]=3
d1$Pi[d1$Es=="THA_2007"&d1$IMD3005_3==7640008]=4
d1$Pi[d1$Es=="THA_2007"&d1$IMD3005_3==7640060]=5
d1$Pi[d1$Es=="THA_2007"&d1$IMD3005_3==7640024]=6

d1$Pi[d1$Es=="THA_2011"&d1$IMD3005_3==7640001]=1
d1$Pi[d1$Es=="THA_2011"&d1$IMD3005_3==7640002]=2
d1$Pi[d1$Es=="THA_2011"&d1$IMD3005_3==7640003]=3
d1$Pi[d1$Es=="THA_2011"&d1$IMD3005_3==7640004]=4
d1$Pi[d1$Es=="THA_2011"&d1$IMD3005_3==7640005]=5
d1$Pi[d1$Es=="THA_2011"&d1$IMD3005_3==7640006]=6

d1$Pi[d1$Es=="TWN_1996"&d1$IMD3005_3==1580002]=1#
d1$Pi[d1$Es=="TWN_1996"&d1$IMD3005_3==1580001]=2
d1$Pi[d1$Es=="TWN_1996"&d1$IMD3005_3==1580004]=3

d1$Pi[d1$Es=="ZAF_2009"&d1$IMD3005_3==7100001]=1#
d1$Pi[d1$Es=="ZAF_2009"&d1$IMD3005_3==7100002]=2
d1$Pi[d1$Es=="ZAF_2009"&d1$IMD3005_3==7100008]=3
d1$Pi[d1$Es=="ZAF_2009"&d1$IMD3005_3==7100004]=4
d1$Pi[d1$Es=="ZAF_2009"&d1$IMD3005_3==7100022]=5
d1$Pi[d1$Es=="ZAF_2009"&d1$IMD3005_3==7100006]=6

d1$Pi[d1$Es=="ZAF_2014"&d1$IMD3005_3==7100001]=1#
d1$Pi[d1$Es=="ZAF_2014"&d1$IMD3005_3==7100002]=2
d1$Pi[d1$Es=="ZAF_2014"&d1$IMD3005_3==7100003]=3
d1$Pi[d1$Es=="ZAF_2014"&d1$IMD3005_3==7100004]=4
d1$Pi[d1$Es=="ZAF_2014"&d1$IMD3005_3==7100005]=5
d1$Pi[d1$Es=="ZAF_2014"&d1$IMD3005_3==7100006]=6


#D5----
#Election variables:
colnames(d5)[colnames(d5)=="E1001"]="Ed"#Election Dataset
colnames(d5)[colnames(d5)=="E1004"]="Es"#Election Study
d5$Es[d5$Es=="GRC_2015"]="GRC_2016"#temporarily modifying 2015s year to code all the variables with
#unique election-year, it will be modified before the analysis
d5$Ec=substr(d5$Es, 1, 3)#Election Country
colnames(d5)[colnames(d5)=="E1008"]="Ey"#Election Year
d5$Ey[d5$Es=="GRC_2016"]=2016#temporarily modifying 2015s year to code all the variables with
#unique election-year, it will be modified before the analysis
colnames(d5)[colnames(d5)=="E1005"]="Rid"#Respondent id
colnames(d5)[colnames(d5)=="E2001_Y"]="Rage"#Respondent Age (E2001_Y is year of birth, get age detracting it to Ey)
d5$Rage[d5$Rage>2020]=NA
d5$Rage=d5$Ey-d5$Rage
d5$Rage[d5$Rage>115]=NA
d5$Rwd=1#Respondent Weight Demographic:
d5$Rwd[d5$Es=="AUS_2019"]=d5$E1010_1[d5$Es=="AUS_2019"]
d5$Rwd[d5$Es=="AUT_2017"]=d5$E1010_2[d5$Es=="AUT_2017"]
d5$Rwd[d5$Es=="DEU_2017"]=d5$E1010_2[d5$Es=="DEU_2017"]
d5$Rwd[d5$Es=="GRC_2016"]=d5$E1010_2[d5$Es=="GRC_2016"]
d5$Rwd[d5$Es=="IRL_2016"]=d5$E1010_2[d5$Es=="IRL_2016"]
d5$Rwd[d5$Es=="ITA_2018"]=d5$E1010_2[d5$Es=="ITA_2018"]
d5$Rwd[d5$Es=="NOR_2017"]=d5$E1010_2[d5$Es=="NOR_2017"]
d5$Rwd[d5$Es=="NZL_2017"]=d5$E1010_2[d5$Es=="NZL_2017"]
#Vote and party/leader features:
colnames(d5)[colnames(d5)=="E3012_LH"]="Vt"#Vote Turnout (0=non voted; 1=voted) CURRENT LOWER HOUSE ELECTION: DID RESPONDENT CAST A BALLOT
d5$Vt[d5$Vt>1]=NA#coding missing as NAs
colnames(d5)[colnames(d5)=="E3013_LH_PL"]="Vn"#Vote Numerical CURRENT LOWER HOUSE ELECTION: VOTE CHOICE - PARTY LIST
d5$Vn[d5$Es=="IRL_2016"]=d5$E3013_LH_DC[d5$Es=="IRL_2016"]#assigning LH_DC to Vn for relevant elections
d5$Vn[d5$Es=="AUS_2019"]=d5$E3013_LH_DC[d5$Es=="AUS_2019"]
d5$Va=NA#Vote Alphabetical: code vote from numerical (000001-999987) to alphabetical modified (1:6):
#d5 assign alphabetical modified codes instead than numerical codes----

d5$Va[d5$Es=="AUS_2019"&d5$Vn==036001]=1##
d5$Va[d5$Es=="AUS_2019"&d5$Vn==036002]=2
d5$Va[d5$Es=="AUS_2019"&d5$Vn==036003]=3
d5$Va[d5$Es=="AUS_2019"&d5$Vn==036004]=4
d5$Va[d5$Es=="AUS_2019"&d5$Vn==036005]=5
d5$Va[d5$Es=="AUS_2019"&d5$Vn==036006]=6

d5$Va[d5$Es=="AUT_2017"&d5$Vn==040001]=1#
d5$Va[d5$Es=="AUT_2017"&d5$Vn==040002]=2
d5$Va[d5$Es=="AUT_2017"&d5$Vn==040003]=3
d5$Va[d5$Es=="AUT_2017"&d5$Vn==040004]=4
d5$Va[d5$Es=="AUT_2017"&d5$Vn==040005]=5
d5$Va[d5$Es=="AUT_2017"&d5$Vn==040006]=6

d5$Va[d5$Es=="DEU_2017"&d5$Vn==276009]=1#vote for either CDU,CSU (same comments than for 2760001)
d5$Va[d5$Es=="DEU_2017"&d5$Vn==276002]=2
d5$Va[d5$Es=="DEU_2017"&d5$Vn==276003]=3
d5$Va[d5$Es=="DEU_2017"&d5$Vn==276004]=4
d5$Va[d5$Es=="DEU_2017"&d5$Vn==276005]=5
d5$Va[d5$Es=="DEU_2017"&d5$Vn==276006]=6

d5$Va[d5$Es=="GRC_2016"&d5$Vn==300001]=1#
d5$Va[d5$Es=="GRC_2016"&d5$Vn==300002]=2
d5$Va[d5$Es=="GRC_2016"&d5$Vn==300003]=3
d5$Va[d5$Es=="GRC_2016"&d5$Vn==300004]=4
d5$Va[d5$Es=="GRC_2016"&d5$Vn==300005]=5
d5$Va[d5$Es=="GRC_2016"&d5$Vn==300006]=6

d5$Va[d5$Es=="IRL_2016"&d5$Vn==372001]=1#
d5$Va[d5$Es=="IRL_2016"&d5$Vn==372002]=2
d5$Va[d5$Es=="IRL_2016"&d5$Vn==372003]=3
d5$Va[d5$Es=="IRL_2016"&d5$Vn==372004]=4
d5$Va[d5$Es=="IRL_2016"&d5$Vn==372005]=5
d5$Va[d5$Es=="IRL_2016"&d5$Vn==372006]=6

d5$Va[d5$Es=="ISL_2016"&d5$Vn==352001]=1##
d5$Va[d5$Es=="ISL_2016"&d5$Vn==352002]=2
d5$Va[d5$Es=="ISL_2016"&d5$Vn==352003]=3
d5$Va[d5$Es=="ISL_2016"&d5$Vn==352004]=4
d5$Va[d5$Es=="ISL_2016"&d5$Vn==352005]=5
d5$Va[d5$Es=="ISL_2016"&d5$Vn==352006]=6

d5$Va[d5$Es=="ISL_2017"&d5$Vn==352101]=1##
d5$Va[d5$Es=="ISL_2017"&d5$Vn==352102]=2
d5$Va[d5$Es=="ISL_2017"&d5$Vn==352103]=3
d5$Va[d5$Es=="ISL_2017"&d5$Vn==352104]=4
d5$Va[d5$Es=="ISL_2017"&d5$Vn==352105]=5
d5$Va[d5$Es=="ISL_2017"&d5$Vn==352106]=6

d5$Va[d5$Es=="ITA_2018"&d5$Vn==380001]=1#Movimento 5 Stelle
d5$Va[d5$Es=="ITA_2018"&d5$Vn==380002]=2#Partito Democratico (PD)
d5$Va[d5$Es=="ITA_2018"&d5$Vn==380003]=3#Lega (LN)
d5$Va[d5$Es=="ITA_2018"&d5$Vn==380004]=4
d5$Va[d5$Es=="ITA_2018"&d5$Vn==380005]=5
d5$Va[d5$Es=="ITA_2018"&d5$Vn==380006]=6

d5$Va[d5$Es=="NOR_2017"&d5$Vn==578001]=1##
d5$Va[d5$Es=="NOR_2017"&d5$Vn==578002]=2
d5$Va[d5$Es=="NOR_2017"&d5$Vn==578003]=3
d5$Va[d5$Es=="NOR_2017"&d5$Vn==578004]=4
d5$Va[d5$Es=="NOR_2017"&d5$Vn==578005]=5
d5$Va[d5$Es=="NOR_2017"&d5$Vn==578006]=6

d5$Va[d5$Es=="NZL_2017"&d5$Vn==554001]=1##
d5$Va[d5$Es=="NZL_2017"&d5$Vn==554002]=2
d5$Va[d5$Es=="NZL_2017"&d5$Vn==554003]=3
d5$Va[d5$Es=="NZL_2017"&d5$Vn==554004]=4
d5$Va[d5$Es=="NZL_2017"&d5$Vn==554005]=5
d5$Va[d5$Es=="NZL_2017"&d5$Vn==554006]=6

colnames(d5)[colnames(d5) %in% paste0("E3017_", LETTERS[1:6])]=paste0("Pl.", 1:6)#party likability [0,10] A-F into 1:6----
d5[,names(d5) %in% paste0("Pl.", 1:6)][d5[,names(d5) %in% paste0("Pl.", 1:6)]>10]=NA#coding missing as NAs

colnames(d5)[colnames(d5) %in% paste0("E3018_", LETTERS[1:6])]=paste0("Ll.", 1:6)#leader likability [0,10] A-F into 1:6
d5[,names(d5) %in% paste0("Ll.", 1:6)][d5[,names(d5) %in% paste0("Ll.", 1:6)]>10]=NA#coding missing as NAs

colnames(d5)[colnames(d5) %in% paste0("E5002_", LETTERS[1:6])]=paste0("Ps.", 1:6)#party seats A-F into 1:6 PERCENT SEATS - LOWER HOUSE
d5[,names(d5) %in% paste0("Ps.", 1:6)][d5[,names(d5) %in% paste0("Ps.", 1:6)]>100]=NA#coding missing as NAs

colnames(d5)[colnames(d5) %in% paste0("E5001_", LETTERS[1:6])]=paste0("Pv.", 1:6)#party vote A-F into 1:6 PERCENT VOTE - LOWER HOUSE
d5[,names(d5) %in% paste0("Pv.", 1:6)][d5[,names(d5) %in% paste0("Pv.", 1:6)]>100]=NA#coding missing as NAs

#EEeconS Election Economy Subjective:
colnames(d5)[colnames(d5)=="E3011"]="EeconS"#Election economy state Subjective (no data from CSES#3!!!)
d5$EeconS[d5$EeconS>5]=NA#coding missing as NAs

#recode EeconS from CSES scale
# 1. GOTTEN BETTER c(1,2,3,4,5)
# 5. GOTTEN WORSE 
# to TEV scale, noting that the TEV used the conversion scale below for same elections:
# 0. Negative evaluation c(0, 0.25, 0.5, 0.75, 1)
# 1. Positive evaluation
d5$EeconS[d5$EeconS==1]=1
d5$EeconS[d5$EeconS==2]=0.75
d5$EeconS[d5$EeconS==3]=0.5
d5$EeconS[d5$EeconS==4]=0.25
d5$EeconS[d5$EeconS==5]=0

#Elections with EeconS:
table(d5$Es,d5$EeconS)
```

```
##           
##               0 0.25  0.5 0.75    1
##   AUS_2019  115  661  845  315   41
##   AUT_2017   28  123  468  449   81
##   DEU_2017    8  108  679  440   60
##   GRC_2016  602  246  187   31    1
##   IRL_2016   31   45  244  551  128
##   ISL_2016   16   50  317  642  227
##   ISL_2017   13   56  672 1003  234
##   ITA_2018  233  413  823  482   27
##   NOR_2017    0  204 1040  535    0
##   NZL_2017   87  264  598  558  122
```

``` r
d5$Eecon=1

#LRR=Left Right Respondent (voter LR self):
d5$LRR=d5$E3020
d5$LRR[d5$LRR>10]=NA
#recoding to 0-10 scale: already 0-10 scale
#LRP.i=Left Right Parties:
d5$LRP.1=d5$E3019_A#LEFT-RIGHT - PARTY A
d5$LRP.2=d5$E3019_B#LEFT-RIGHT - PARTY B
d5$LRP.3=d5$E3019_C#LEFT-RIGHT - PARTY C
d5$LRP.4=d5$E3019_D#LEFT-RIGHT - PARTY D
d5$LRP.5=d5$E3019_E#LEFT-RIGHT - PARTY E
d5$LRP.6=d5$E3019_F#LEFT-RIGHT - PARTY F
# d5$LRP.7=as.numeric(NA)
d5$LRP.1[d5$LRP.1>10]=NA
d5$LRP.2[d5$LRP.2>10]=NA
d5$LRP.3[d5$LRP.3>10]=NA
d5$LRP.4[d5$LRP.4>10]=NA
d5$LRP.5[d5$LRP.5>10]=NA
d5$LRP.6[d5$LRP.6>10]=NA
#recoding to 0-10 scale: already 0-10 scale
#Elections with LRR and LRP:
d5$LR=1

#Seats to vote ratios:
d5$P1SVrat=d5$Ps.1/d5$Pv.1#for most voted party
d5$P2SVrat=d5$Ps.2/d5$Pv.2#for second most voted party
d5$P1P2rat=d5$P1SVrat/d5$P2SVrat#ratio of rations first/second party
#Gallagher Disproportionality measure:
d5$GDM=NA
d5$GDM[d5$Es=="AUT_2017"]=3.73
d5$GDM[d5$Es=="DEU_2017"]=1.95#
d5$GDM[d5$Es=="GRC_2016"]=9.60#Sep
d5$GDM[d5$Es=="IRL_2016"]=5.62
d5$GDM[d5$Es=="ITA_2018"]=5.70

d5$GDM[d5$Es=="AUS_2019"]=12.35#
d5$GDM[d5$Es=="ISL_2016"]=4.46#
d5$GDM[d5$Es=="ISL_2017"]=1.91#
d5$GDM[d5$Es=="NOR_2017"]=3.01#
d5$GDM[d5$Es=="NZL_2017"]=2.73#

#d5 assign CSES numerical ID to alternatives (CSESid.i):----
d5$CSESid.1=NA
d5$CSESid.2=NA
d5$CSESid.3=NA
d5$CSESid.4=NA
d5$CSESid.5=NA
d5$CSESid.6=NA
# d5$CSESid.7=NA

d5$CSESid.1[d5$Es=="AUS_2019"]=036001
d5$CSESid.2[d5$Es=="AUS_2019"]=036002
d5$CSESid.3[d5$Es=="AUS_2019"]=036003
d5$CSESid.4[d5$Es=="AUS_2019"]=036004
d5$CSESid.5[d5$Es=="AUS_2019"]=036005
d5$CSESid.6[d5$Es=="AUS_2019"]=036006

d5$CSESid.1[d5$Es=="AUT_2017"]=040001
d5$CSESid.2[d5$Es=="AUT_2017"]=040002
d5$CSESid.3[d5$Es=="AUT_2017"]=040003
d5$CSESid.4[d5$Es=="AUT_2017"]=040004
d5$CSESid.5[d5$Es=="AUT_2017"]=040005
d5$CSESid.6[d5$Es=="AUT_2017"]=040006

d5$CSESid.1[d5$Es=="DEU_2017"]=276009#vote for either CDU,CSU (same comments than for 2760001)
d5$CSESid.2[d5$Es=="DEU_2017"]=276002
d5$CSESid.3[d5$Es=="DEU_2017"]=276003
d5$CSESid.4[d5$Es=="DEU_2017"]=276004
d5$CSESid.5[d5$Es=="DEU_2017"]=276005
d5$CSESid.6[d5$Es=="DEU_2017"]=276006

d5$CSESid.1[d5$Es=="GRC_2016"]=300001
d5$CSESid.2[d5$Es=="GRC_2016"]=300002
d5$CSESid.3[d5$Es=="GRC_2016"]=300003
d5$CSESid.4[d5$Es=="GRC_2016"]=300004
d5$CSESid.5[d5$Es=="GRC_2016"]=300005
d5$CSESid.6[d5$Es=="GRC_2016"]=300006

d5$CSESid.1[d5$Es=="IRL_2016"]=372001
d5$CSESid.2[d5$Es=="IRL_2016"]=372002
d5$CSESid.3[d5$Es=="IRL_2016"]=372003
d5$CSESid.4[d5$Es=="IRL_2016"]=372004
d5$CSESid.5[d5$Es=="IRL_2016"]=372005
d5$CSESid.6[d5$Es=="IRL_2016"]=372006

d5$CSESid.1[d5$Es=="ISL_2016"]=352001
d5$CSESid.2[d5$Es=="ISL_2016"]=352002
d5$CSESid.3[d5$Es=="ISL_2016"]=352003
d5$CSESid.4[d5$Es=="ISL_2016"]=352004
d5$CSESid.5[d5$Es=="ISL_2016"]=352005
d5$CSESid.6[d5$Es=="ISL_2016"]=352006

d5$CSESid.1[d5$Es=="ISL_2017"]=352101
d5$CSESid.2[d5$Es=="ISL_2017"]=352102
d5$CSESid.3[d5$Es=="ISL_2017"]=352103
d5$CSESid.4[d5$Es=="ISL_2017"]=352104
d5$CSESid.5[d5$Es=="ISL_2017"]=352105
d5$CSESid.6[d5$Es=="ISL_2017"]=352106

d5$CSESid.1[d5$Es=="ITA_2018"]=380001
d5$CSESid.2[d5$Es=="ITA_2018"]=380002
d5$CSESid.3[d5$Es=="ITA_2018"]=380003
d5$CSESid.4[d5$Es=="ITA_2018"]=380004
d5$CSESid.5[d5$Es=="ITA_2018"]=380005
d5$CSESid.6[d5$Es=="ITA_2018"]=380006

d5$CSESid.1[d5$Es=="NOR_2017"]=578001
d5$CSESid.2[d5$Es=="NOR_2017"]=578002
d5$CSESid.3[d5$Es=="NOR_2017"]=578003
d5$CSESid.4[d5$Es=="NOR_2017"]=578004
d5$CSESid.5[d5$Es=="NOR_2017"]=578005
d5$CSESid.6[d5$Es=="NOR_2017"]=578006

d5$CSESid.1[d5$Es=="NZL_2017"]=554001
d5$CSESid.2[d5$Es=="NZL_2017"]=554002
d5$CSESid.3[d5$Es=="NZL_2017"]=554003
d5$CSESid.4[d5$Es=="NZL_2017"]=554004
d5$CSESid.5[d5$Es=="NZL_2017"]=554005
d5$CSESid.6[d5$Es=="NZL_2017"]=554006


#d5 party identification (Pi):----

d5$Pi=NA

#assign Pi:
d5$Pi[d5$Es=="AUS_2019"&d5$E3024_3==036001]=1##
d5$Pi[d5$Es=="AUS_2019"&d5$E3024_3==036002]=2
d5$Pi[d5$Es=="AUS_2019"&d5$E3024_3==036003]=3
d5$Pi[d5$Es=="AUS_2019"&d5$E3024_3==036004]=4
d5$Pi[d5$Es=="AUS_2019"&d5$E3024_3==036005]=5
d5$Pi[d5$Es=="AUS_2019"&d5$E3024_3==036006]=6

d5$Pi[d5$Es=="AUT_2017"&d5$E3024_3==040001]=1#
d5$Pi[d5$Es=="AUT_2017"&d5$E3024_3==040002]=2
d5$Pi[d5$Es=="AUT_2017"&d5$E3024_3==040003]=3
d5$Pi[d5$Es=="AUT_2017"&d5$E3024_3==040004]=4
d5$Pi[d5$Es=="AUT_2017"&d5$E3024_3==040005]=5
d5$Pi[d5$Es=="AUT_2017"&d5$E3024_3==040006]=6

d5$Pi[d5$Es=="DEU_2017"&d5$E3024_3==276009]=1#vote for either CDU,CSU (same comments than for 2760001)
d5$Pi[d5$Es=="DEU_2017"&d5$E3024_3==276002]=2
d5$Pi[d5$Es=="DEU_2017"&d5$E3024_3==276003]=3
d5$Pi[d5$Es=="DEU_2017"&d5$E3024_3==276004]=4
d5$Pi[d5$Es=="DEU_2017"&d5$E3024_3==276005]=5
d5$Pi[d5$Es=="DEU_2017"&d5$E3024_3==276006]=6

d5$Pi[d5$Es=="GRC_2016"&d5$E3024_3==300001]=1#
d5$Pi[d5$Es=="GRC_2016"&d5$E3024_3==300002]=2
d5$Pi[d5$Es=="GRC_2016"&d5$E3024_3==300003]=3
d5$Pi[d5$Es=="GRC_2016"&d5$E3024_3==300004]=4
d5$Pi[d5$Es=="GRC_2016"&d5$E3024_3==300005]=5
d5$Pi[d5$Es=="GRC_2016"&d5$E3024_3==300006]=6

d5$Pi[d5$Es=="IRL_2016"&d5$E3024_3==372001]=1#
d5$Pi[d5$Es=="IRL_2016"&d5$E3024_3==372002]=2
d5$Pi[d5$Es=="IRL_2016"&d5$E3024_3==372003]=3
d5$Pi[d5$Es=="IRL_2016"&d5$E3024_3==372004]=4
d5$Pi[d5$Es=="IRL_2016"&d5$E3024_3==372005]=5
d5$Pi[d5$Es=="IRL_2016"&d5$E3024_3==372006]=6

d5$Pi[d5$Es=="ISL_2016"&d5$E3024_3==352001]=1##
d5$Pi[d5$Es=="ISL_2016"&d5$E3024_3==352002]=2
d5$Pi[d5$Es=="ISL_2016"&d5$E3024_3==352003]=3
d5$Pi[d5$Es=="ISL_2016"&d5$E3024_3==352004]=4
d5$Pi[d5$Es=="ISL_2016"&d5$E3024_3==352005]=5
d5$Pi[d5$Es=="ISL_2016"&d5$E3024_3==352006]=6

d5$Pi[d5$Es=="ISL_2017"&d5$E3024_3==352101]=1##
d5$Pi[d5$Es=="ISL_2017"&d5$E3024_3==352102]=2
d5$Pi[d5$Es=="ISL_2017"&d5$E3024_3==352103]=3
d5$Pi[d5$Es=="ISL_2017"&d5$E3024_3==352104]=4
d5$Pi[d5$Es=="ISL_2017"&d5$E3024_3==352105]=5
d5$Pi[d5$Es=="ISL_2017"&d5$E3024_3==352106]=6

d5$Pi[d5$Es=="ITA_2018"&d5$E3024_3==380001]=1#Movimento 5 Stelle
d5$Pi[d5$Es=="ITA_2018"&d5$E3024_3==380002]=2#Partito Democratico (PD)
d5$Pi[d5$Es=="ITA_2018"&d5$E3024_3==380003]=3#Lega (LN)
d5$Pi[d5$Es=="ITA_2018"&d5$E3024_3==380004]=4
d5$Pi[d5$Es=="ITA_2018"&d5$E3024_3==380005]=5
d5$Pi[d5$Es=="ITA_2018"&d5$E3024_3==380006]=6

d5$Pi[d5$Es=="NOR_2017"&d5$E3024_3==578001]=1##
d5$Pi[d5$Es=="NOR_2017"&d5$E3024_3==578002]=2
d5$Pi[d5$Es=="NOR_2017"&d5$E3024_3==578003]=3
d5$Pi[d5$Es=="NOR_2017"&d5$E3024_3==578004]=4
d5$Pi[d5$Es=="NOR_2017"&d5$E3024_3==578005]=5
d5$Pi[d5$Es=="NOR_2017"&d5$E3024_3==578006]=6

d5$Pi[d5$Es=="NZL_2017"&d5$E3024_3==554001]=1##
d5$Pi[d5$Es=="NZL_2017"&d5$E3024_3==554002]=2
d5$Pi[d5$Es=="NZL_2017"&d5$E3024_3==554003]=3
d5$Pi[d5$Es=="NZL_2017"&d5$E3024_3==554004]=4
d5$Pi[d5$Es=="NZL_2017"&d5$E3024_3==554005]=5
d5$Pi[d5$Es=="NZL_2017"&d5$E3024_3==554006]=6


#MERGE DATASETS----

colnames(d1)
```

```
##   [1] "Rid"             "Ed"              "IMD1002_VER"     "IMD1002_DOI"     "IMD1003"        
##   [6] "Es"              "IMD1006"         "IMD1006_UN"      "IMD1006_NAM"     "IMD1007"        
##  [11] "Ey"              "IMD1008_MOD_1"   "IMD1008_MOD_2"   "IMD1008_MOD_3"   "IMD1008_MOD_4"  
##  [16] "IMD1008_RES"     "IMD1009"         "IMD1010_1"       "IMD1010_2"       "IMD1010_3"      
##  [21] "IMD1011_M"       "IMD1011_D"       "IMD1011_Y"       "IMD1012_M"       "IMD1012_D"      
##  [26] "IMD1012_Y"       "IMD1013_M"       "IMD1013_D"       "IMD1013_Y"       "IMD1014_1"      
##  [31] "IMD1014_2"       "IMD1015"         "IMD1016_1"       "IMD1016_2"       "IMD1016_3"      
##  [36] "Rage"            "IMD2001_2"       "IMD2002"         "IMD2003"         "IMD2004"        
##  [41] "IMD2005"         "IMD2006"         "IMD2007"         "IMD3001"         "IMD3001_PR_1"   
##  [46] "IMD3001_PR_2"    "Vt"              "IMD3001_UH"      "IMD3002_PR_1"    "IMD3002_PR_2"   
##  [51] "Vn"              "IMD3002_LH_DC"   "IMD3002_UH_PL"   "IMD3002_UH_DC_1" "IMD3002_UH_DC_2"
##  [56] "IMD3002_UH_DC_3" "IMD3002_UH_DC_4" "IMD3002_OUTGOV"  "IMD3003_PR_1"    "IMD3003_PR_2"   
##  [61] "IMD3003_LH"      "IMD3003_UH"      "IMD3004_PR_1"    "IMD3004_PR_2"    "IMD3004_LH_PL"  
##  [66] "IMD3004_LH_DC"   "IMD3004_UH_PL"   "IMD3004_UH_DC_1" "IMD3004_UH_DC_2" "IMD3004_UH_DC_3"
##  [71] "IMD3005_1"       "IMD3005_2"       "IMD3005_3"       "IMD3005_4"       "IMD3006"        
##  [76] "IMD3007_A"       "IMD3007_B"       "IMD3007_C"       "IMD3007_D"       "IMD3007_E"      
##  [81] "IMD3007_F"       "IMD3007_G"       "IMD3007_H"       "IMD3007_I"       "Pl.1"           
##  [86] "Pl.2"            "Pl.3"            "Pl.4"            "Pl.5"            "Pl.6"           
##  [91] "IMD3008_G"       "IMD3008_H"       "IMD3008_I"       "Ll.1"            "Ll.2"           
##  [96] "Ll.3"            "Ll.4"            "Ll.5"            "Ll.6"            "IMD3009_G"      
## [101] "IMD3009_H"       "IMD3009_I"       "IMD3010"         "IMD3011"         "IMD3012"        
## [106] "IMD3013_1"       "IMD3013_2"       "IMD3013_3"       "IMD3014"         "IMD3015_1"      
## [111] "IMD3015_2"       "IMD3015_3"       "IMD3015_4"       "IMD3015_A"       "IMD3015_B"      
## [116] "IMD3015_C"       "IMD3015_D"       "IMD5000_A"       "IMD5000_B"       "IMD5000_C"      
## [121] "IMD5000_D"       "IMD5000_E"       "IMD5000_F"       "IMD5000_G"       "IMD5000_H"      
## [126] "IMD5000_I"       "Pv.1"            "Pv.2"            "Pv.3"            "Pv.4"           
## [131] "Pv.5"            "Pv.6"            "IMD5001_G"       "IMD5001_H"       "IMD5001_I"      
## [136] "Ps.1"            "Ps.2"            "Ps.3"            "Ps.4"            "Ps.5"           
## [141] "Ps.6"            "IMD5002_G"       "IMD5002_H"       "IMD5002_I"       "IMD5003_A"      
## [146] "IMD5003_B"       "IMD5003_C"       "IMD5003_D"       "IMD5003_E"       "IMD5003_F"      
## [151] "IMD5003_G"       "IMD5003_H"       "IMD5003_I"       "IMD5004_A"       "IMD5004_B"      
## [156] "IMD5004_C"       "IMD5004_D"       "IMD5004_E"       "IMD5004_F"       "IMD5004_G"      
## [161] "IMD5004_H"       "IMD5004_I"       "IMD5005_A"       "IMD5005_B"       "IMD5005_C"      
## [166] "IMD5005_D"       "IMD5005_E"       "IMD5005_F"       "IMD5005_G"       "IMD5005_H"      
## [171] "IMD5005_I"       "IMD5006_1"       "IMD5006_2"       "IMD5007"         "IMD5008_1"      
## [176] "IMD5008_2"       "IMD5009_1"       "IMD5009_2"       "IMD5011_A"       "IMD5011_B"      
## [181] "IMD5011_C"       "IMD5011_D"       "IMD5011_E"       "IMD5011_F"       "IMD5011_G"      
## [186] "IMD5011_H"       "IMD5011_I"       "IMD5012_A"       "IMD5012_B"       "IMD5012_C"      
## [191] "IMD5012_D"       "IMD5012_E"       "IMD5012_F"       "IMD5012_G"       "IMD5012_H"      
## [196] "IMD5012_I"       "IMD5013"         "IMD5014"         "IMD5016_1"       "IMD5016_2"      
## [201] "IMD5016_3"       "IMD5016_4"       "IMD5017_1"       "IMD5017_2"       "IMD5017_3"      
## [206] "IMD5017_4"       "IMD5018_1"       "IMD5018_2"       "IMD5018_3"       "IMD5018_4"      
## [211] "IMD5021_1"       "IMD5021_2"       "IMD5021_3"       "IMD5021_4"       "IMD5022_1"      
## [216] "IMD5022_2"       "IMD5022_3"       "IMD5022_4"       "IMD5024_1"       "IMD5024_2"      
## [221] "IMD5024_3"       "IMD5025_1"       "IMD5025_2"       "IMD5025_3"       "IMD5026_1"      
## [226] "IMD5026_2"       "IMD5027"         "IMD5028"         "IMD5029_A"       "IMD5029_B"      
## [231] "IMD5029_C"       "IMD5029_D"       "IMD5029_E"       "IMD5029_F"       "IMD5029_G"      
## [236] "IMD5029_H"       "IMD5029_I"       "IMD5030"         "IMD5031_A"       "IMD5031_B"      
## [241] "IMD5031_C"       "IMD5031_D"       "IMD5031_E"       "IMD5031_F"       "IMD5031_G"      
## [246] "IMD5031_H"       "IMD5031_I"       "IMD5049"         "IMD5050_1"       "IMD5050_2"      
## [251] "IMD5050_3"       "IMD5051_1"       "IMD5051_2"       "IMD5051_3"       "IMD5052_1"      
## [256] "IMD5052_2"       "IMD5052_3"       "IMD5053_1"       "IMD5053_2"       "IMD5053_3"      
## [261] "IMD5054_1"       "IMD5054_2"       "IMD5054_3"       "IMD5055_1"       "IMD5055_2"      
## [266] "IMD5055_3"       "IMD5056_1"       "IMD5056_2"       "IMD5056_3"       "IMD5057_1"      
## [271] "IMD5057_2"       "IMD5057_3"       "IMD5058_1"       "IMD5058_2"       "IMD5059_1"      
## [276] "IMD5059_2"       "NP"              "CoalCode"        "Ec"              "Rwd"            
## [281] "Va"              "EeconS"          "Eecon"           "LRR"             "LRP.1"          
## [286] "LRP.2"           "LRP.3"           "LRP.4"           "LRP.5"           "LRP.6"          
## [291] "LR"              "P1SVrat"         "P2SVrat"         "P1P2rat"         "GDM"            
## [296] "CSESid.1"        "CSESid.2"        "CSESid.3"        "CSESid.4"        "CSESid.5"       
## [301] "CSESid.6"        "Pi"
```

``` r
colnames(d5)
```

```
##   [1] "Ed"            "E1002_VER"     "E1002_DOI"     "E1003"         "Es"            "Rid"          
##   [7] "E1006"         "E1006_UN"      "E1006_NAM"     "E1007"         "Ey"            "E1009"        
##  [13] "E1010_1"       "E1010_2"       "E1010_3"       "E1011_1"       "E1011_2"       "E1011_3"      
##  [19] "E1012_1"       "E1012_2"       "E1012_3"       "E1013"         "E1014_1"       "E1014_2"      
##  [25] "E1014_3"       "E1015"         "E1016"         "E1017"         "E1018"         "E1019"        
##  [31] "E1020"         "E1021"         "E1022"         "E1023"         "E1024_1"       "E1024_2"      
##  [37] "E1024_3"       "E1025_1"       "E1025_2"       "E1025_3"       "E1026"         "E1027"        
##  [43] "E1028"         "E1029"         "E1030"         "E1031"         "E1032"         "E1033"        
##  [49] "E1034"         "E1035_1"       "E1035_2"       "E1036"         "E1037"         "E2001_M"      
##  [55] "Rage"          "E2002"         "E2003"         "E2004"         "E2005"         "E2006"        
##  [61] "E2007"         "E2008"         "E2009"         "E2010"         "E2011"         "E2012"        
##  [67] "E2013"         "E2014"         "E2015"         "E2016"         "E2017"         "E2018"        
##  [73] "E2019"         "E2020"         "E2021"         "E2022"         "E3001"         "E3002"        
##  [79] "E3003"         "E3004_1"       "E3004_1_PT"    "E3004_2"       "E3004_3"       "E3004_4"      
##  [85] "E3004_5"       "E3004_6"       "E3004_7"       "E3004_8_PT"    "E3005_1"       "E3005_2"      
##  [91] "E3005_3"       "E3005_4"       "E3005_5"       "E3006_1"       "E3006_2"       "E3006_3"      
##  [97] "E3006_4"       "E3006_5_PT"    "E3006_6_PT"    "E3006_7_PT"    "E3006_8_PT"    "E3007"        
## [103] "E3008"         "E3008_PT"      "E3009"         "E3010_1"       "E3010_2"       "EeconS"       
## [109] "E3012_PR_1"    "E3012_PR_2"    "Vt"            "E3012_UH"      "E3013_PR_1"    "E3013_PR_2"   
## [115] "Vn"            "E3013_LH_DC"   "E3013_LH_PF"   "E3013_UH_PL"   "E3013_UH_DC_1" "E3013_UH_DC_2"
## [121] "E3013_UH_PF"   "E3013_OUTGOV"  "E3014_PR_1"    "E3014_PR_2"    "E3014_LH"      "E3014_UH"     
## [127] "E3015_PR_1"    "E3015_PR_2"    "E3015_LH_PL"   "E3015_LH_DC"   "E3015_UH_PL"   "E3015_UH_DC_1"
## [133] "E3015_UH_DC_2" "E3016_1"       "E3016_2"       "Pl.1"          "Pl.2"          "Pl.3"         
## [139] "Pl.4"          "Pl.5"          "Pl.6"          "E3017_G"       "E3017_H"       "E3017_I"      
## [145] "Ll.1"          "Ll.2"          "Ll.3"          "Ll.4"          "Ll.5"          "Ll.6"         
## [151] "E3018_G"       "E3018_H"       "E3018_I"       "E3019_A"       "E3019_B"       "E3019_C"      
## [157] "E3019_D"       "E3019_E"       "E3019_F"       "E3019_G"       "E3019_H"       "E3019_I"      
## [163] "E3020"         "E3021_A"       "E3021_B"       "E3021_C"       "E3021_D"       "E3021_E"      
## [169] "E3021_F"       "E3021_G"       "E3021_H"       "E3021_I"       "E3022"         "E3023"        
## [175] "E3024_1"       "E3024_2"       "E3024_3"       "E3024_4"       "E4001"         "E4001_N"      
## [181] "E4002"         "E4002_N"       "E4003"         "E4003_N"       "E4004_A"       "E4004_B"      
## [187] "E4004_C"       "E4004_D"       "E4004_E"       "E4004_F"       "E4004_G"       "E4004_H"      
## [193] "E4004_I"       "E4004_A_N"     "E4004_B_N"     "E4004_C_N"     "E4004_D_N"     "E4004_E_N"    
## [199] "E4004_F_N"     "E4004_G_N"     "E4004_H_N"     "E4004_I_N"     "E4005_A"       "E4005_B"      
## [205] "E4005_C"       "E4005_D"       "E4005_E"       "E4005_F"       "E4005_G"       "E4005_H"      
## [211] "E4005_I"       "E4005_A_N"     "E4005_B_N"     "E4005_C_N"     "E4005_D_N"     "E4005_E_N"    
## [217] "E4005_F_N"     "E4005_G_N"     "E4005_H_N"     "E4005_I_N"     "E4006"         "E4006_N"      
## [223] "E4007"         "E4007_N"       "E5000_A"       "E5000_B"       "E5000_C"       "E5000_D"      
## [229] "E5000_E"       "E5000_F"       "E5000_G"       "E5000_H"       "E5000_I"       "Pv.1"         
## [235] "Pv.2"          "Pv.3"          "Pv.4"          "Pv.5"          "Pv.6"          "E5001_G"      
## [241] "E5001_H"       "E5001_I"       "Ps.1"          "Ps.2"          "Ps.3"          "Ps.4"         
## [247] "Ps.5"          "Ps.6"          "E5002_G"       "E5002_H"       "E5002_I"       "E5003_A"      
## [253] "E5003_B"       "E5003_C"       "E5003_D"       "E5003_E"       "E5003_F"       "E5003_G"      
## [259] "E5003_H"       "E5003_I"       "E5004_A"       "E5004_B"       "E5004_C"       "E5004_D"      
## [265] "E5004_E"       "E5004_F"       "E5004_G"       "E5004_H"       "E5004_I"       "E5005_A"      
## [271] "E5005_B"       "E5005_C"       "E5005_D"       "E5005_E"       "E5005_F"       "E5005_G"      
## [277] "E5005_H"       "E5005_I"       "E5006_1"       "E5006_2"       "E5007_1"       "E5007_2"      
## [283] "E5008_1"       "E5008_2"       "E5008_3"       "E5009"         "E5010"         "E5011_A"      
## [289] "E5011_B"       "E5011_C"       "E5011_D"       "E5011_E"       "E5011_F"       "E5011_G"      
## [295] "E5011_H"       "E5011_I"       "E5012"         "E5013"         "E5014"         "E5015_A"      
## [301] "E5015_B"       "E5015_C"       "E5015_D"       "E5015_E"       "E5015_F"       "E5015_G"      
## [307] "E5015_H"       "E5015_I"       "E5016"         "E5017_A"       "E5017_B"       "E5017_C"      
## [313] "E5017_D"       "E5017_E"       "E5017_F"       "E5017_G"       "E5017_H"       "E5017_I"      
## [319] "E5018_A"       "E5018_B"       "E5018_C"       "E5018_D"       "E5018_E"       "E5018_F"      
## [325] "E5018_G"       "E5018_H"       "E5018_I"       "E5019"         "E5019_A"       "E5019_B"      
## [331] "E5019_C"       "E5019_D"       "E5019_E"       "E5019_F"       "E5019_G"       "E5019_H"      
## [337] "E5019_I"       "E5020"         "E5020_A"       "E5020_B"       "E5020_C"       "E5020_D"      
## [343] "E5020_E"       "E5020_F"       "E5020_G"       "E5020_H"       "E5020_I"       "E5021_1"      
## [349] "E5021_2"       "E5021_3"       "E5021_4"       "E5021_5"       "E5022"         "E5023"        
## [355] "E5024"         "E5025_1"       "E5025_2"       "E5025_3"       "E5026_1"       "E5026_2"      
## [361] "E5026_3"       "E5027"         "E5028"         "E5029"         "E5030"         "E5031"        
## [367] "E5032"         "E5033"         "E5034"         "E5035"         "E5036"         "E5037"        
## [373] "E5038"         "E5039"         "E5040_1"       "E5040_2"       "E5040_3"       "E5040_4"      
## [379] "E5041_1"       "E5041_2"       "E5041_3"       "E5041_4"       "E5042_1"       "E5042_2"      
## [385] "E5042_3"       "E5042_4"       "E5043_1"       "E5043_2"       "E5043_3"       "E5043_4"      
## [391] "E5044_1"       "E5044_2"       "E5044_3"       "E5044_4"       "E5045_1"       "E5045_2"      
## [397] "E5045_3"       "E5045_4"       "E5046_1"       "E5046_2"       "E5046_3"       "E5046_4"      
## [403] "E5047_1"       "E5047_2"       "E5047_3"       "E5047_4"       "E5048_1"       "E5048_2"      
## [409] "E5048_3"       "E5048_4"       "E5049_1"       "E5049_2"       "E5049_3"       "E5049_4"      
## [415] "E5050"         "E5051"         "E5052"         "E5053"         "E5054"         "E5055"        
## [421] "E5056"         "E5057"         "E5058"         "E5059"         "E5060"         "E5061"        
## [427] "E5062"         "E5063"         "E5064"         "E5065"         "E5066"         "E5067"        
## [433] "E5068"         "E5069"         "E5070"         "E5071"         "E5072"         "E5073"        
## [439] "E5074"         "E5075"         "E5076_1"       "E5076_2"       "E5077"         "E5078"        
## [445] "E5079"         "E5080"         "E5081"         "E5082_1"       "E5082_2"       "E5082_3"      
## [451] "E5082_4"       "E5090_1"       "E5090_2"       "E5090_3"       "E5091_1"       "E5091_2"      
## [457] "E5091_3"       "E5092"         "E5093_1"       "E5093_2"       "E5093_3"       "E5094_1"      
## [463] "E5094_2"       "E5094_3"       "E5095_1"       "E5095_2"       "E5095_3"       "E5096_1"      
## [469] "E5096_2"       "E5096_3"       "E5097_1"       "E5097_2"       "E5097_3"       "E5098_1"      
## [475] "E5098_2"       "E5098_3"       "E5099_1"       "E5099_2"       "E5099_3"       "E5100_1"      
## [481] "E5100_2"       "E5100_3"       "E5101"         "E5102"         "E5103_1"       "E5103_1se"    
## [487] "E5103_2"       "E5103_2se"     "E5103_3"       "E5103_3se"     "E5104_1"       "E5104_2"      
## [493] "E5104_3"       "E5104_4"       "E5105_1"       "E5105_2"       "E5105_3"       "E5105_4"      
## [499] "E5106_1"       "E5106_2"       "E5106_3"       "E5107"         "E5108"         "E5109"        
## [505] "E5110"         "E5111"         "E5112"         "E5113"         "E5200_A"       "E5200_B"      
## [511] "E5200_C"       "E5200_D"       "E5200_E"       "E5200_F"       "E5200_G"       "E5200_H"      
## [517] "E5200_I"       "E5201_A"       "E5201_B"       "E5201_C"       "E5201_D"       "E5201_E"      
## [523] "E5201_F"       "E5201_G"       "E5201_H"       "E5201_I"       "NP"            "CoalCode"     
## [529] "Ec"            "Rwd"           "Va"            "Eecon"         "LRR"           "LRP.1"        
## [535] "LRP.2"         "LRP.3"         "LRP.4"         "LRP.5"         "LRP.6"         "LR"           
## [541] "P1SVrat"       "P2SVrat"       "P1P2rat"       "GDM"           "CSESid.1"      "CSESid.2"     
## [547] "CSESid.3"      "CSESid.4"      "CSESid.5"      "CSESid.6"      "Pi"
```

``` r
d=rbind(d1[,c(279,11,6,2,1,280,36,277,278,47,281,85:90,94:99,284,285:290,282,294,295,51,296:301,127:132,136:141,302)]
        ,d5[,c(529,11,5,1,6,530,55,527,528,111,531,136:141,145:150,533,534:539,108,543,544,115,545:550,234:239,243:248,551)])
colnames(d)
```

```
##  [1] "Ec"       "Ey"       "Es"       "Ed"       "Rid"      "Rwd"      "Rage"     "NP"       "CoalCode"
## [10] "Vt"       "Va"       "Pl.1"     "Pl.2"     "Pl.3"     "Pl.4"     "Pl.5"     "Pl.6"     "Ll.1"    
## [19] "Ll.2"     "Ll.3"     "Ll.4"     "Ll.5"     "Ll.6"     "LRR"      "LRP.1"    "LRP.2"    "LRP.3"   
## [28] "LRP.4"    "LRP.5"    "LRP.6"    "EeconS"   "P1P2rat"  "GDM"      "Vn"       "CSESid.1" "CSESid.2"
## [37] "CSESid.3" "CSESid.4" "CSESid.5" "CSESid.6" "Pv.1"     "Pv.2"     "Pv.3"     "Pv.4"     "Pv.5"    
## [46] "Pv.6"     "Ps.1"     "Ps.2"     "Ps.3"     "Ps.4"     "Ps.5"     "Ps.6"     "Pi"
```

``` r
rm(d1,d11,d12,d13,d14,d5)


#CMPid:----

#read party Id match CSES-CMP:
dID=read_csv("CSES to CMP by party ID.csv")
```

```
## New names:
## Rows: 1239 Columns: 9
## ── Column specification
## ────────────────────────────────────────────────────────────────────────────────────── Delimiter: "," chr
## (4): id, country, Party, party dbl (5): ...1, elecyear, num_id, manifestoid, rile
## ℹ Use `spec()` to retrieve the full column specification for this data. ℹ Specify the column types or set
## `show_col_types = FALSE` to quiet this message.
## • `` -> `...1`
```

``` r
dID=dID[,c(3,4,7,8)]#filter data and set variable names:
colnames(dID)[colnames(dID)=="num_id"]="CSESid"#"Vn"
colnames(dID)[colnames(dID)=="manifestoid"]="CMPid"
colnames(dID)[colnames(dID)=="elecyear"]="Ey"
colnames(dID)[colnames(dID)=="country"]="Ec"

#code party family alternative i (CMPid.i):
d=merge(d, dID, by.x=c("Ey","Ec","CSESid.1"), by.y=c("Ey","Ec","CSESid"), all.x=T)#alternative 1
colnames(d)[colnames(d)=="CMPid"]="CMPid.1"
d=merge(d, dID, by.x=c("Ey","Ec","CSESid.2"), by.y=c("Ey","Ec","CSESid"), all.x=T)#alternative 2
colnames(d)[colnames(d)=="CMPid"]="CMPid.2"
d=merge(d, dID, by.x=c("Ey","Ec","CSESid.3"), by.y=c("Ey","Ec","CSESid"), all.x=T)#alternative 3
colnames(d)[colnames(d)=="CMPid"]="CMPid.3"
d=merge(d, dID, by.x=c("Ey","Ec","CSESid.4"), by.y=c("Ey","Ec","CSESid"), all.x=T)#alternative 4
colnames(d)[colnames(d)=="CMPid"]="CMPid.4"
d=merge(d, dID, by.x=c("Ey","Ec","CSESid.5"), by.y=c("Ey","Ec","CSESid"), all.x=T)#alternative 5
colnames(d)[colnames(d)=="CMPid"]="CMPid.5"
d=merge(d, dID, by.x=c("Ey","Ec","CSESid.6"), by.y=c("Ey","Ec","CSESid"), all.x=T)#alternative 6
colnames(d)[colnames(d)=="CMPid"]="CMPid.6"


#CMPrile:----

#read CMP dataset:
dCMP=read.xlsx("MPDataset_MPDS2020b.xlsx")

#filter CMP dataset and set variable names:
colnames(dCMP)[colnames(dCMP)=="party"]="CMPid_temp"
colnames(dCMP)[colnames(dCMP)=="rile"]="CMPrile"

dCMP$Ec=NA
dCMP$Ec=as.character(dCMP$Ec)
dCMP$Ec[dCMP$country==63]="AUS"
dCMP$Ec[dCMP$country==42]="AUT"
dCMP$Ec[dCMP$country==62]="CAN"
dCMP$Ec[dCMP$country==41]="DEU"
dCMP$Ec[dCMP$country==13]="DNK"
dCMP$Ec[dCMP$country==33]="ESP"
dCMP$Ec[dCMP$country==14]="FIN"
dCMP$Ec[dCMP$country==51]="GBR"
dCMP$Ec[dCMP$country==34]="GRC"
dCMP$Ec[dCMP$country==53]="IRL"
dCMP$Ec[dCMP$country==15]="ISL"
dCMP$Ec[dCMP$country==72]="ISR"
dCMP$Ec[dCMP$country==32]="ITA"
dCMP$Ec[dCMP$country==22]="NLD"
dCMP$Ec[dCMP$country==12]="NOR"
dCMP$Ec[dCMP$country==64]="NZL"
dCMP$Ec[dCMP$country==35]="PRT"
dCMP$Ec[dCMP$country==11]="SWE"
dCMP$Ec[dCMP$country==83]="EST"#
dCMP$Ec[dCMP$country==86]="HUN"
dCMP$Ec[dCMP$country==88]="LTU"
dCMP$Ec[dCMP$country==97]="SVN"

#GRC_2012: there was an election one month before in May, need to exclude it from dCMP
# to merge in dt only the parties in the June election (which is the one coded by CSES):
dCMP=dCMP[!(dCMP$Ec=="GRC"&dCMP$date==201205),]

dCMP$Ey=as.numeric(substr(dCMP$date,1,4))
dCMP$Ey[dCMP$Ec=="GRC"&dCMP$date==201509]=2016#set temporary Ey for double elections

dCMP$Es=paste0(dCMP$Ec,"_",dCMP$Ey)

#keep only variables of interest:
dCMP=dCMP[,c(175,176,7,168)]

#code pervote alternative i (CMPrile.i):
d=merge(d, dCMP, by.x=c("Ec","Ey","CMPid.1"), by.y=c("Ec","Ey","CMPid_temp"), all.x=T)
colnames(d)[colnames(d)=="CMPrile"]="CMPrile.1"
d=merge(d, dCMP, by.x=c("Ec","Ey","CMPid.2"), by.y=c("Ec","Ey","CMPid_temp"), all.x=T)
colnames(d)[colnames(d)=="CMPrile"]="CMPrile.2"
d=merge(d, dCMP, by.x=c("Ec","Ey","CMPid.3"), by.y=c("Ec","Ey","CMPid_temp"), all.x=T)
colnames(d)[colnames(d)=="CMPrile"]="CMPrile.3"
d=merge(d, dCMP, by.x=c("Ec","Ey","CMPid.4"), by.y=c("Ec","Ey","CMPid_temp"), all.x=T)
colnames(d)[colnames(d)=="CMPrile"]="CMPrile.4"
d=merge(d, dCMP, by.x=c("Ec","Ey","CMPid.5"), by.y=c("Ec","Ey","CMPid_temp"), all.x=T)
colnames(d)[colnames(d)=="CMPrile"]="CMPrile.5"
d=merge(d, dCMP, by.x=c("Ec","Ey","CMPid.6"), by.y=c("Ec","Ey","CMPid_temp"), all.x=T)
colnames(d)[colnames(d)=="CMPrile"]="CMPrile.6"


#CMPper415:----

#read CMP dataset:
dCMP=read.xlsx("MPDataset_MPDS2020b.xlsx")

#filter CMP dataset and set variable names:
colnames(dCMP)[colnames(dCMP)=="party"]="CMPid_temp"
colnames(dCMP)[colnames(dCMP)=="per415"]="CMPper415"

dCMP$Ec=NA
dCMP$Ec=as.character(dCMP$Ec)
dCMP$Ec[dCMP$country==63]="AUS"
dCMP$Ec[dCMP$country==42]="AUT"
dCMP$Ec[dCMP$country==62]="CAN"
dCMP$Ec[dCMP$country==41]="DEU"
dCMP$Ec[dCMP$country==13]="DNK"
dCMP$Ec[dCMP$country==33]="ESP"
dCMP$Ec[dCMP$country==14]="FIN"
dCMP$Ec[dCMP$country==51]="GBR"
dCMP$Ec[dCMP$country==34]="GRC"
dCMP$Ec[dCMP$country==53]="IRL"
dCMP$Ec[dCMP$country==15]="ISL"
dCMP$Ec[dCMP$country==72]="ISR"
dCMP$Ec[dCMP$country==32]="ITA"
dCMP$Ec[dCMP$country==22]="NLD"
dCMP$Ec[dCMP$country==12]="NOR"
dCMP$Ec[dCMP$country==64]="NZL"
dCMP$Ec[dCMP$country==35]="PRT"
dCMP$Ec[dCMP$country==11]="SWE"
dCMP$Ec[dCMP$country==83]="EST"#
dCMP$Ec[dCMP$country==86]="HUN"
dCMP$Ec[dCMP$country==88]="LTU"
dCMP$Ec[dCMP$country==97]="SVN"

#GRC_2012: there was an election one month before in May, need to exclude it from dCMP
# to merge in dt only the parties in the June election (which is the one coded by CSES):
dCMP=dCMP[!(dCMP$Ec=="GRC"&dCMP$date==201205),]

dCMP$Ey=as.numeric(substr(dCMP$date,1,4))
dCMP$Ey[dCMP$Ec=="GRC"&dCMP$date==201509]=2016#set temporary Ey for double elections

dCMP$Es=paste0(dCMP$Ec,"_",dCMP$Ey)

#keep only variables of interest:
dCMP=dCMP[,c(175,176,7,59)]

#code pervote alternative i (CMPper415.i):
d=merge(d, dCMP, by.x=c("Ec","Ey","CMPid.1"), by.y=c("Ec","Ey","CMPid_temp"), all.x=T)
colnames(d)[colnames(d)=="CMPper415"]="CMPper415.1"
d=merge(d, dCMP, by.x=c("Ec","Ey","CMPid.2"), by.y=c("Ec","Ey","CMPid_temp"), all.x=T)
colnames(d)[colnames(d)=="CMPper415"]="CMPper415.2"
d=merge(d, dCMP, by.x=c("Ec","Ey","CMPid.3"), by.y=c("Ec","Ey","CMPid_temp"), all.x=T)
colnames(d)[colnames(d)=="CMPper415"]="CMPper415.3"
d=merge(d, dCMP, by.x=c("Ec","Ey","CMPid.4"), by.y=c("Ec","Ey","CMPid_temp"), all.x=T)
colnames(d)[colnames(d)=="CMPper415"]="CMPper415.4"
d=merge(d, dCMP, by.x=c("Ec","Ey","CMPid.5"), by.y=c("Ec","Ey","CMPid_temp"), all.x=T)
colnames(d)[colnames(d)=="CMPper415"]="CMPper415.5"
d=merge(d, dCMP, by.x=c("Ec","Ey","CMPid.6"), by.y=c("Ec","Ey","CMPid_temp"), all.x=T)
colnames(d)[colnames(d)=="CMPper415"]="CMPper415.6"


#VPf:----

#read CMP data and party Id match CSES-CMP:
dID=read_csv("CSES to CMP by party ID.csv")
```

```
## New names:
## Rows: 1239 Columns: 9
## ── Column specification
## ────────────────────────────────────────────────────────────────────────────────────── Delimiter: "," chr
## (4): id, country, Party, party dbl (5): ...1, elecyear, num_id, manifestoid, rile
## ℹ Use `spec()` to retrieve the full column specification for this data. ℹ Specify the column types or set
## `show_col_types = FALSE` to quiet this message.
## • `` -> `...1`
```

``` r
dCMP=read.xlsx("MPDataset_MPDS2020b.xlsx")

dID=dID[,c(7,8)]#filter data and set variable names:
colnames(dID)[colnames(dID)=="num_id"]="CSESid"#"Vn"
colnames(dID)[colnames(dID)=="manifestoid"]="CMPid"
dID=dID[!duplicated(dID$CSESid),]

dCMP=dCMP[,c(7,10)]#filter data and set variable names:
colnames(dCMP)[colnames(dCMP)=="party"]="CMPid"
colnames(dCMP)[colnames(dCMP)=="parfam"]="Pf"
dCMP=dCMP[!duplicated(dCMP$CMPid),]

#code party family vote choice (VPf):
d=merge(d, dID, by.x=c("Vn"), by.y=c("CSESid"), all.x=T)
d=merge(d, dCMP, by = c("CMPid"), all.x=T)
# sort(unique(d__$Vn[is.na(d__$Pf)]))#the missing Pf are in general for voters for parties that are not the top 6 and hence will be dropped out later on
colnames(d)[colnames(d)=="Pf"]="VPf"
d=d[,-which(names(d) %in% c("CMPid"))]

rm(dID,dCMP)


#PrePost:----
d$PrePost=as.numeric(1)

# 1. Post-election study
# 2. Pre-election study
# 3. Panel, with some pre-election and some post-election data


#Ed election dataset:----
d$Ed="CSES"


#SET DOUBLE ELECTIONS BACK TO ORIGINAL VALUES:----

#settign GRC 2015 elections back to correct year and election study:
d$Es[d$Ec=="GRC"&d$Ey==2015]="GRC_2015j"#January election
d$Es[d$Ec=="GRC"&d$Ey==2016]="GRC_2015s"#September election
d$Ey[d$Ec=="GRC"&d$Ey==2016]=2015


#SAVE DATA:----
colnames(d)
```

```
##  [1] "Vn"          "Ec"          "Ey"          "CMPid.6"     "CMPid.5"     "CMPid.4"     "CMPid.3"    
##  [8] "CMPid.2"     "CMPid.1"     "CSESid.6"    "CSESid.5"    "CSESid.4"    "CSESid.3"    "CSESid.2"   
## [15] "CSESid.1"    "Es"          "Ed"          "Rid"         "Rwd"         "Rage"        "NP"         
## [22] "CoalCode"    "Vt"          "Va"          "Pl.1"        "Pl.2"        "Pl.3"        "Pl.4"       
## [29] "Pl.5"        "Pl.6"        "Ll.1"        "Ll.2"        "Ll.3"        "Ll.4"        "Ll.5"       
## [36] "Ll.6"        "LRR"         "LRP.1"       "LRP.2"       "LRP.3"       "LRP.4"       "LRP.5"      
## [43] "LRP.6"       "EeconS"      "P1P2rat"     "GDM"         "Pv.1"        "Pv.2"        "Pv.3"       
## [50] "Pv.4"        "Pv.5"        "Pv.6"        "Ps.1"        "Ps.2"        "Ps.3"        "Ps.4"       
## [57] "Ps.5"        "Ps.6"        "Pi"          "CMPrile.1"   "CMPrile.2"   "CMPrile.3"   "CMPrile.4"  
## [64] "CMPrile.5"   "CMPrile.6"   "CMPper415.1" "CMPper415.2" "CMPper415.3" "CMPper415.4" "CMPper415.5"
## [71] "CMPper415.6" "VPf"         "PrePost"
```

``` r
d=d[,c(2:3,16:24,1,25:46,72:73,47:58,60:65,15:4,66:71,59)]
save(d, file = "d29_CSES.RData")
```

